Class and instance question
rzed
rzantow at gmail.com
Sun Dec 17 10:49:50 EST 2006
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
More information about the Python-list
mailing list