467,925 Members | 1,754 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,925 developers. It's quick & easy.

Class and instance question

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.
>>print T.a
1
>>print y.a
4
>>print z.a
4

So far, it's as I expect, but:
>>print x.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'a'
>>print t.a
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?

--
rzed
Dec 17 '06 #1
  • viewed: 855
Share:
4 Replies
rzed <rz*****@gmail.comwrites:

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

t = T()

and then you get
>>print t.a
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

Dec 17 '06 #2
rzed wrote:
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.
>>>print T.a
1
>>>print y.a
4
>>>print z.a
4

So far, it's as I expect, but:
>>>print x.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'a'
>>>print t.a
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.

Dec 17 '06 #3
Colin J. Williams wrote:
rzed wrote:
>class T(object):
def __new__(self):
self.a = 1
...
t = T()

and I want to examine the 'a' attributes.
>>>>print T.a
1
>>>>print t.a
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
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:
>>t = T()
print repr(t)
newt = NewT()
print repr(newt)
T.a
t.a
--Scott David Daniels
sc***********@acm.org
Dec 18 '06 #4
Marco Wahl a écrit :
(snip)
The __new__ method should return the class.
s/class/instance/
Dec 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Murat Tasan | last post: by
7 posts views Thread by Kerry Neilson | last post: by
1 post views Thread by Scott Jacobsen | last post: by
20 posts views Thread by tshad | last post: by
4 posts views Thread by Bit Byter | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.