Reputation: 117
May I know why myClass1
and myClass2
behaves differently in overriding the __new__()
method? Which way is recommended to write a class and why? I think myClass1():
does not even call __new__(cls)
, am I right?
$ python
Python 2.7.5+ (default, Sep 19 2013, 13:49:51)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class myClass1():
... def __new__(cls):
... print cls.__name__
...
>>> class myClass2(object):
... def __new__(cls):
... print cls.__name__
...
>>> o1 = myClass1()
>>> o2 = myClass2()
myClass2
>>>
Upvotes: 1
Views: 72
Reputation: 1123400
When you inherit from object
you are creating a new-style class. Not inheriting from object
makes it an old-style class.
Old-style classes don't support the __new__
constructor.
In Python 3, all classes are new-style and the object
base class is implicit if there are no explicit base classes specified.
Upvotes: 5