472,353 Members | 1,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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
3 1860
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michele Simionato | last post by:
Let me show first how does it work for tuples: >>> class MyTuple(tuple): .... def __new__(cls,strng): # implicit conversion string of ints =>...
8
by: Simon Burton | last post by:
Python 2.2.2 (#2, Nov 24 2002, 11:41:06) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class...
10
by: Stuart McGraw | last post by:
I have a class A from a third party that I cannot change and is implemented in C. I derive my own class B from A and add a couple new methods and...
1
by: Adonis | last post by:
I am subclassing the dict object, and having it serialize itself to disk for later access, but I am having some trouble with accessing the...
5
by: could ildg | last post by:
As there is already __init__, why need a __new__? What can __new__ give us while __init__ can't? In what situations we should use __new__? And in...
3
by: James Stroud | last post by:
Hello All, I'm running 2.3.4 I was reading the documentation for classes & types http://www.python.org/2.2.3/descrintro.html And stumbled on...
5
by: Ken Schutte | last post by:
Hi, I'm been trying to create some custom classes derived from some of python's built-in types, like int and list, etc. I've run into some...
6
by: c james | last post by:
Given a condition at the time a class is instantiated, I want to change how __call__ is used. From the example below, self.no is using self.yes...
4
by: Steven D'Aprano | last post by:
When you call a new-style class, the __new__ method is called with the user-supplied arguments, followed by the __init__ method with the same...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.