Overriding the __new__ method

Gerrit gerrit at nl.linux.org
Sun Feb 8 12:41:38 EST 2004


Christoph Groth wrote:
> According to my understanding, if float type's __init__ ignores its
> arguments then the value of `arg' above should be ignored.  Instead,
> the constructed object has the value 12.  Where is this 12 passed to
> the object being constructed?

To __new__.

>>> class Test(object):
...  def __new__(cls):
...   print "calling new"
...   return object.__new__(cls)
...  def __init__(self):
...   print "calling init"
...
>>> Test()
calling new
calling init
<__main__.Test object at 0xbf491f0c>

...they are both called, but it is constructed in __new__. __new__
returns the object, while __init__ returns nothing. __init__ is actually
called after initialization, because the initialization happens when
object.__new__ is called. Because it is an immutable type, it cannot be
changed after it has been initialized, so __init__ can't thange it.

Gerrit.

-- 
PrePEP: Builtin path type
    http://people.nl.linux.org/~gerrit/creaties/path/pep-xxxx.html
Asperger's Syndrome - a personal approach:
	http://people.nl.linux.org/~gerrit/english/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040208/f981a7ad/attachment.sig>


More information about the Python-list mailing list