Class and instance question

Marco Wahl marco.wahl at gmail.com
Sun Dec 17 11:16:50 EST 2006


rzed <rzantow at gmail.com> writes:

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




More information about the Python-list mailing list