views:

52

answers:

1

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?

+2  A: 

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?-).

Alex Martelli
Sorry, my mistake :( You are right..What's the proper thing to do? Should I delete the question?
max
I'm using __new__ to implement enumerator (see this question of mine: http://stackoverflow.com/questions/3588996/python-enumeration-class-for-orm-purposes)
max
@max, just clarify that you're calling `object.__new__(cls)` by editing your question -- **that** `cls` argument makes a `cls` instance, which is why `cls.__init__` is then called on it, as it must.
Alex Martelli