Overriding the __new__ method

Discussion in 'Python' started by Christoph Groth, Feb 8, 2004.

  1. -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.0.6 (GNU/Linux)
    Comment: For info see http://www.gnupg.org

    iD8DBQBAJmKufx2/njzvX5URAipnAKCUco5ZCl85xJ8YboHHJM3RBIqiGwCfX3/P
    tL1uWVMKntHThZ550BS32aw=
    =4Ijl
    -----END PGP SIGNATURE-----
    Christoph Groth, Feb 8, 2004
    #1
    1. Advertising

  2. Christoph Groth

    Peter Otten Guest

    Christoph Groth wrote:

    > 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:


    No, it doesn't.
    >
    > 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


    So one inch is one meter?

    Python 2.3.3 (#1, Jan 3 2004, 13:57:08)
    [GCC 3.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class inch(float):

    .... def __new__(cls, v):
    .... return float.__new__(cls, v*0.0254)
    ....
    >>> inch(12)

    0.30479999999999996

    See the difference?

    Peter
    Peter Otten, Feb 8, 2004
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.0.6 (GNU/Linux)
    Comment: For info see http://www.gnupg.org

    iD8DBQBAJm/mfx2/njzvX5URAnuoAJwMQDInysC9xNa7CvOOFB8dp1KpEACgpote
    l6SuUSUeG1fKS0uyWmsWD7Y=
    =gX+0
    -----END PGP SIGNATURE-----
    Christoph Groth, Feb 8, 2004
    #3
  4. Christoph Groth

    Gerrit Guest

    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/

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.3 (GNU/Linux)

    iD8DBQFAJnTS6A1yqfkBKlARAsR3AJ4/qeyhDo/ER5ktqoZuaYuEDlcq5gCeOYBp
    NEDtLRF92iry2fOLSnKMLM4=
    =nfj2
    -----END PGP SIGNATURE-----
    Gerrit, Feb 8, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michele Simionato

    Overriding list.__new__

    Michele Simionato, Jul 3, 2003, in forum: Python
    Replies:
    1
    Views:
    329
    Terry Reedy
    Jul 3, 2003
  2. Steven Bethard
    Replies:
    2
    Views:
    455
    Steven Bethard
    Feb 16, 2005
  3. Andreas Otto
    Replies:
    2
    Views:
    343
  4. Marco
    Replies:
    1
    Views:
    176
    Ian Kelly
    Nov 20, 2012
  5. Jurko Gospodnetiæ

    Why has __new__ been implemented as a static method?

    Jurko Gospodnetiæ, May 3, 2014, in forum: Python
    Replies:
    7
    Views:
    92
    Ian Kelly
    May 4, 2014
Loading...

Share This Page