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:
>>class Spam(object):
.... def __new__(cls, *args):
.... print "__new__", args
.... x = object.__new__( cls)
.... args = ['spam spam spam']
.... return x
.... def __init__(self, *args):
.... print "__init__", args # hope to get 'spam spam spam'
.... return None
but naturally it doesn't work:
>>s = Spam('spam and eggs', 'tomato', 'beans are off')
__new__ ('spam and eggs', 'tomato', 'beans are off')
__init__ ('spam and eggs', 'tomato', 'beans are off')
Is there any way to do this, or am I all outta luck?
--
Steven 4 3533
On Dec 22, 11:03 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
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.
Only if __new__ returns an object of the type passed into __new__.
Otherwise, __init__ is not called.
I would like to modify the arguments after the __new__ method is called
but before the __init__ method, somewhat like this:
What's your use-case? I mean, why not just do this in __init__
instead of __new__?
>class Spam(object):
... def __new__(cls, *args):
... print "__new__", args
... x = object.__new__( cls)
... args = ['spam spam spam']
... return x
... def __init__(self, *args):
... print "__init__", args # hope to get 'spam spam spam'
... return None
but naturally it doesn't work:
>s = Spam('spam and eggs', 'tomato', 'beans are off')
__new__ ('spam and eggs', 'tomato', 'beans are off')
__init__ ('spam and eggs', 'tomato', 'beans are off')
Is there any way to do this, or am I all outta luck?
From what I can tell from http://docs.python.org/ref/customization.html,
you are out of luck doing it this way unless you jury rig some way to
have __new__ return an object of a different type.
--Nathan Davis
Steven D'Aprano wrote:
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:
>>>class Spam(object):
... def __new__(cls, *args):
... print "__new__", args
... x = object.__new__( cls)
... args = ['spam spam spam']
... return x
... def __init__(self, *args):
... print "__init__", args # hope to get 'spam spam spam'
... return None
but naturally it doesn't work:
>>>s = Spam('spam and eggs', 'tomato', 'beans are off')
__new__ ('spam and eggs', 'tomato', 'beans are off')
__init__ ('spam and eggs', 'tomato', 'beans are off')
Is there any way to do this, or am I all outta luck?
You can really only achieve this by writing a metaclass. When a new
object is created, what's first called is the __call__ method of the
type object. This basically looks like::
def __call__(cls, *args, **kwargs):
obj = cls.__new__(cls , *args, **kwargs)
obj.__init__(*a rgs, **kwargs)
return obj
Hopefully that explains the behavior you're seeing. If you want
different behavior than this, you can change __call__ by defining your
own metaclass with a different __call__ method, for example::
>>class SpamMeta(type):
... def __call__(cls, *args, **kwargs):
... obj = cls.__new__(cls )
... obj.__init__('s pam spam spam')
... return obj
...
>>class Spam(object):
... __metaclass__ = SpamMeta
... def __new__(cls, *args):
... print '__new__', args
... return object.__new__( cls)
... def __init__(self, *args):
... print '__init__', args
...
>>Spam()
__new__ ()
__init__ ('spam spam spam',)
<__main__.Spa m object at 0x00E756F0>
Hope that helps,
STeVe
On Dec 23, 5:03*am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
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:
>class Spam(object):
... * * def __new__(cls, *args):
... * * * * * * print "__new__", args
... * * * * * * x = object.__new__( cls)
... * * * * * * args = ['spam spam spam']
... * * * * * * return x
... * * def __init__(self, *args):
... * * * * * * print "__init__", args *# hope to get 'spam spam spam'
... * * * * * * return None
but naturally it doesn't work:
>s = Spam('spam and eggs', 'tomato', 'beans are off')
__new__ ('spam and eggs', 'tomato', 'beans are off')
__init__ ('spam and eggs', 'tomato', 'beans are off')
Is there any way to do this, or am I all outta luck?
--
Steven
The ususal way is to override the __call__ method of the metaclass.
HTH
--
Arnaud
On Sat, 22 Dec 2007 23:01:50 -0700, Steven Bethard wrote:
Steven D'Aprano wrote:
>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:
[snip]
You can really only achieve this by writing a metaclass. When a new
object is created, what's first called is the __call__ method of the
type object. This basically looks like::
[snip]
That's an excellent explanation of how to use metaclasses!
Thanks Steve, and everyone else who answered. I'm not yet sure if that's
the approach I'm going to use (I may end up moving all the instance code
into __new__, or __init__, rather than splitting it) but that's an
interesting option for me to explore.
--
Steven This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Peter Olsen |
last post by:
I want to define a class "point" as a subclass of complex.
When I create an instance
sample = point(<arglist>)
I want "sample" to "be" a complex number, but with its real and
imaginary parts computed in point()'s __init__ function with their
values based on the arglist. I want to compute with point instances
as though they were native complex numbers, but I want to be able to
|
by: Mark English |
last post by:
I'd like to write a Tkinter app which, given a class, pops up a
window(s) with fields for each "attribute" of that class. The user could
enter values for the attributes and on closing the window would be
returned an instance of the class. The actual application I'm interested
in writing would either have simple type attributes (int, string, etc.),
or attributes using types already defined in a c-extension, although I'd
prefer not to...
|
by: Felix Wiemann |
last post by:
Sometimes (but not always) the __new__ method of one of my classes
returns an *existing* instance of the class. However, when it does
that, the __init__ method of the existing instance is called
nonetheless, so that the instance is initialized a second time. For
example, please consider the following class (a singleton in this case):
>>> class C(object):
.... instance = None
.... def __new__(cls):
.... if C.instance is None:
|
by: Steven Bethard |
last post by:
Felix Wiemann wrote:
> Steven Bethard wrote:
>> http://www.python.org/2.2.3/descrintro.html#__new__
>
>
> I'm just seeing that the web page says:
>
> | If you return an existing object, the constructor call will still
> | call its __init__ method. If you return an object of a different
|
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: Frank Benkstein |
last post by:
Hi,
the behaviour I always observed when creating instances by calling the
class A is that '__init__' is always only called when the object
returned by A.__new__ is an instance of A. This can be observed by the
following code:
class A(object):
def __new__(cls, *args, **kwds):
print "A.__new__", args, kwds
|
by: Sandra-24 |
last post by:
Ok here's the problem, I'm modifying a 3rd party library (boto) to
have more specific exceptions. I want to change S3ResponseError into
about 30 more specific errors. Preferably I want to do this by
changing as little code as possible. I also want the new exceptions to
be a subclass of the old S3ResponseError so as to not break old code
that catches it. If I meet these two requirements I expect my
modification to make it into boto and then...
|
by: Ethan Furman |
last post by:
Emile van Sebille wrote:
Good point. What I'm curious about, though, is the comment in the code
about making the Decimal instance immutable. I was unable to find docs
on that issue.
~Ethan~
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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: 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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |