python: return value from __new__
EDIT
I actually called object.__new__(cls)
, and I didn't realize that by this I built an object of class cls! Thanks for pointing this out to me.
ORIGINAL QUESTION
The documentation says
If new() does not return an instance of cls, then the new instance’s init() method will not be invoked.
However, when I return object.__new__()
from cls.__new__()
, the __init__()
is still invoked. I wouldn't consider an instance of object to qualify as an instance of cls. What am I missing?
Cannot reproduce your observation:
>>> class cls(object):
... def __new__(cls):
... return object.__new__(object)
... def __init__(self):
... print 'in __init__'
...
>>> x = cls()
>>>
As you see, cls.__init__
isn't executing.
How are you calling object.__new__
(and, btw, why are you?-).
精彩评论