Overriding the __new__ method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christoph Groth

    Overriding the __new__ method

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

    iD8DBQBAJmKufx2/njzvX5URAipnAKC Uco5ZCl85xJ8Ybo HHJM3RBIqiGwCfX 3/P
    tL1uWVMKntHThZ5 50BS32aw=
    =4Ijl
    -----END PGP SIGNATURE-----
  • Peter Otten

    #2
    Re: Overriding the __new__ method

    Christoph Groth wrote:
    [color=blue]
    > 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:
    >[color=green][color=darkred]
    > >>> class inch(float):[/color][/color]
    > ... "THIS DOESN'T WORK!!!"
    > ... def __init__(self, arg=0.0):
    > ... float.__init__( self, arg*0.0254)
    > ...[color=green][color=darkred]
    > >>> print inch(12)[/color][/color]
    > 12.0[color=green][color=darkred]
    > >>>[/color][/color]
    > </quote>
    >
    > Well, I tried this with Python 2.2.1 and it _does_ work:[/color]

    No, it doesn't.[color=blue]
    >
    > 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.[color=green][color=darkred]
    >>>> class inch(float):[/color][/color]
    > ... def __init__(self, arg=0.0):
    > ... float.__init__( self, arg*0.0254)
    > ...[color=green][color=darkred]
    >>>> print inch(12)[/color][/color]
    > 12.0[/color]

    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.[color=blue][color=green][color=darkred]
    >>> class inch(float):[/color][/color][/color]
    .... def __new__(cls, v):
    .... return float.__new__(c ls, v*0.0254)
    ....[color=blue][color=green][color=darkred]
    >>> inch(12)[/color][/color][/color]
    0.3047999999999 9996

    See the difference?

    Peter

    Comment

    • Christoph Groth

      #3
      Re: Overriding the __new__ method

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

      iD8DBQBAJm/mfx2/njzvX5URAnuoAJw MQDInysC9xNa7Cv OOFB8dp1KpEACgp ote
      l6SuUSUeG1fKS0u yWmsWD7Y=
      =gX+0
      -----END PGP SIGNATURE-----

      Comment

      • Gerrit

        #4
        Re: Overriding the __new__ method

        Christoph Groth wrote:[color=blue]
        > 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?[/color]

        To __new__.
        [color=blue][color=green][color=darkred]
        >>> class Test(object):[/color][/color][/color]
        ... def __new__(cls):
        ... print "calling new"
        ... return object.__new__( cls)
        ... def __init__(self):
        ... print "calling init"
        ...[color=blue][color=green][color=darkred]
        >>> Test()[/color][/color][/color]
        calling new
        calling init
        <__main__.Tes t 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

        Asperger's Syndrome - a personal approach:


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

        iD8DBQFAJnTS6A1 yqfkBKlARAsR3AJ 4/qeyhDo/ER5ktqoZuaYuEDl cq5gCeOYBp
        NEDtLRF92iry2fO LSnKMLM4=
        =nfj2
        -----END PGP SIGNATURE-----

        Comment

        Working...