-----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----- 3 1934
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__(c ls, v*0.0254)
.... inch(12)
0.3047999999999 9996
See the difference?
Peter
-----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-----
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__.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 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)
iD8DBQFAJnTS6A1 yqfkBKlARAsR3AJ 4/qeyhDo/ER5ktqoZuaYuEDl cq5gCeOYBp
NEDtLRF92iry2fO LSnKMLM4=
=nfj2
-----END PGP SIGNATURE----- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 super(MyTuple,cls).__new__(cls,map(int,strng.split()))
>>> MyTuple('1 2')
(1, 2)
No wonder here, everything is fine. However, if I do the same for
lists I get the following:
|
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):
.... tuple.__init__(self, (a,b) )
....
>>> a=pair(1,2)
Traceback (most recent call last):
|
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 method (call it A.f() ) that creates
and returns a new A object. I need B.f() to return a B
object derived from A.f(). What is the best way to make
that happen?
|
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 it functions fine, but when I
access it on a later time, self._filename is not present, how can I go about
this knowing that when objects are serialized they are not initialized on
subsequent access? I was under the impression that everything in...
|
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__ take the place of __init__?
Thanks.
| |
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 object. There's nothing that requires that it return a
|
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 examples. Lets say
I want an int-derived class that is initilized to one greater than what
it's constructor is given:
class myint(int):
def __new__(cls, intIn):
|
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 explain why?
EXAMPLE:
class YesNo(object):
def __init__(self, which):
self.no = self.yes
self.__call__ = self.yes
|
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 after the __new__ method is called
but before the __init__ method, somewhat like this:
.... def __new__(cls, *args):
.... print "__new__", args
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |