Class and instance question

R

rzed

I'm confused (not for the first time).

I create these classes:

class T(object):
def __new__(self):
self.a = 1

class X(T):
def __init__(self):
self.a = 4

class Y:
def __init__(self):
self.a = 4

class Z(object):
def __init__(self):
self.a = 4


.... and these instances:

t = T()
x = X()
y = Y()
z = Z()

and I want to examine the 'a' attributes.
4

So far, it's as I expect, but:
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'a'

So what the heck is 'T'? It seems that I can't instantiate it or
derive from it, so I guess it isn't a proper class. But it's
something; it has an attribute. What is it? How would it be used
(or, I guess, how should the __new__() method be used)? Any hints?
 
M

Marco Wahl

To simplify take
class T(object):
def __new__(self):
self.a = 1

and

t = T()

and then you get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'a'

While T.a is 1.
So what the heck is 'T'? It seems that I can't instantiate it or
derive from it, so I guess it isn't a proper class. But it's
something; it has an attribute. What is it?

I don't know.
How would it be used
(or, I guess, how should the __new__() method be used)? Any hints?

The __new__ method should return the class. In your
case return is None. Further the parametername for the
__new__ method should be better cls to have a
distinction to the usual self for instances.

See http://www.python.org/doc/2.4.4/ref/customization.html


Best wishes
 
C

Colin J. Williams

rzed said:
I'm confused (not for the first time).

I create these classes:

class T(object):
def __new__(self):
self.a = 1

class X(T):
def __init__(self):
self.a = 4

class Y:
def __init__(self):
self.a = 4

class Z(object):
def __init__(self):
self.a = 4


... and these instances:

t = T()
x = X()
y = Y()
z = Z()

and I want to examine the 'a' attributes.

4

So far, it's as I expect, but:

Traceback (most recent call last):

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'a'

So what the heck is 'T'? It seems that I can't instantiate it or
derive from it, so I guess it isn't a proper class. But it's
something; it has an attribute. What is it? How would it be used
(or, I guess, how should the __new__() method be used)? Any hints?
__new__ should return something. Since there is no return statement,
None is returned.

You might try something like:

class T(object):
def __new__(cls):
cls= object.__new__(cls)
cls.a= 1
return cls
t= T()
print t.a

Colin W.
 
S

Scott David Daniels

Colin said:
__new__ should return something. Since there is no return statement,
None is returned.

You might try something like:

class T(object):
def __new__(cls):
cls= object.__new__(cls)
cls.a= 1
return cls
t= T()
print t.a

Or, to use a bit more revealing names:
class NewT(object):
def __new__(class_):
instance = object.__new__(class_)
instance.a = 1
return instance

You might have figured more of this out with:

--Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top