467,915 Members | 1,718 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

Overriding the __new__ method

-----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-----
Jul 18 '05 #1
  • viewed: 1758
Share:
3 Replies
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
Jul 18 '05 #2
-----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-----
Jul 18 '05 #3
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/c.../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-----

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Michele Simionato | last post: by
8 posts views Thread by Simon Burton | last post: by
10 posts views Thread by Stuart McGraw | last post: by
1 post views Thread by Adonis | last post: by
5 posts views Thread by could ildg | last post: by
3 posts views Thread by James Stroud | last post: by
5 posts views Thread by Ken Schutte | last post: by
6 posts views Thread by c james | last post: by
4 posts views Thread by Steven D'Aprano | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.