Overriding the __new__ method

Christoph Groth cgroth at gmx.net
Sun Feb 8 17:24:07 CET 2004


Hello,

The essay "Unifying types and classes in Python 2.2"
http://www.python.org/2.2.1/descrintro.html says in section
"Overriding the __new__ method":

<quote>
This class isn't very useful (it's not even the right way to go about
unit conversions) but it shows how to extend the constructor of an
immutable type. If instead of __new__ we had tried to override
__init__, it wouldn't have worked:

    >>> class inch(float):
    ...     "THIS DOESN'T WORK!!!"
    ...     def __init__(self, arg=0.0):
    ...         float.__init__(self, arg*0.0254)
    ...
    >>> print inch(12)
    12.0
    >>> 
</quote>

Well, I tried this with Python 2.2.1 and it _does_ work:

piglet:~$ python
Python 2.2.1 (#1, Sep  7 2002, 14:34:30) 
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class inch(float):
...     def __init__(self, arg=0.0):
...         float.__init__(self, arg*0.0254)
... 
>>> print inch(12)
12.0

Now I'm a bit puzzled.

Thank you,

Christoph
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 238 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040208/99f867e7/attachment.pgp>


More information about the Python-list mailing list