__new__

James Stroud jstroud at mbi.ucla.edu
Sun Nov 6 01:45:37 EST 2005


Hello All,

I'm running 2.3.4

I was reading the documentation for classes & types
       http://www.python.org/2.2.3/descrintro.html
And stumbled on this paragraph:

"""
__new__ must return an object. There's nothing that requires that it return a 
new object that is an instance of its class argument, although that is the 
convention. If you return an existing object, the constructor call will still 
call its __init__ method. If you return an object of a different class, its 
__init__ method will be called.
"""

The quote implies that when I call carol, b.__init__ should be called. 
However, this does not seem to be the case (see code below). What am I not 
understanding? Shouldn't the interpreter call b.__init__ when b is returned 
from carol.__new__?

James

py> class bob(object):
...   def __init__(self):
...     print self.x
...   x = 2
...
py> class carol(object):
...   def __new__(cls):
...     return b
...
py> b=bob()
py> b.x
2
py> c = carol()   # should print "2"
py> c
<__main__.bob object at 0x404333cc>



-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list