Overriding the __new__ method

  • Thread starter Christoph Groth
  • Start date
C

Christoph Groth

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

Peter Otten

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

... "THIS DOESN'T WORK!!!"
... def __init__(self, arg=0.0):
... float.__init__(self, arg*0.0254)
...
</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.... def __init__(self, arg=0.0):
... float.__init__(self, arg*0.0254)
...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..... def __new__(cls, v):
.... return float.__new__(cls, v*0.0254)
....0.30479999999999996

See the difference?

Peter
 
C

Christoph Groth

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

Gerrit

Christoph said:
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__.
... def __new__(cls):
... print "calling new"
... return object.__new__(cls)
... def __init__(self):
... print "calling init"
...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-----
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top