473,574 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Modify arguments between __new__ and __init__

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
Dec 23 '07 #1
4 3526
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
Dec 23 '07 #2
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
Dec 23 '07 #3
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

Dec 23 '07 #4
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
Dec 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1688
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...
8
2524
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,...
9
6200
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): >>>...
2
1486
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
3
2595
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
5
2148
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...
1
1195
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
5
1448
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...
4
1787
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~
0
7835
marktang
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...
0
7753
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...
0
8095
Oralloy
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. ...
0
8265
jinu1996
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...
0
8132
tracyyun
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...
0
6500
agi2029
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...
1
5645
isladogs
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...
0
3787
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1096
bsmnconsultancy
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...

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.