473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 1911
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 => tuple .... return...
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 pair(tuple): .... def __init__(self,a,b): .... ...
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 override a method. The problem is that A has a...
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 self._filename attribute, when the object is first created...
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 what situations we must use __new__? Can __new__...
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 this paragraph: """ __new__ must return an...
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 trouble, which I could explain with a couple simple...
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 but self.__call__ is not. Can someone please...
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 arguments. I would like to modify the arguments...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.