472,146 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 3380
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__(*args, **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__('spam 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__.Spam 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Peter Olsen | last post: by
9 posts views Thread by Felix Wiemann | last post: by
3 posts views Thread by James Stroud | last post: by
5 posts views Thread by Ken Schutte | last post: by
1 post views Thread by Frank Benkstein | last post: by
5 posts views Thread by Sandra-24 | last post: by
4 posts views Thread by Ethan Furman | last post: by
reply views Thread by leo001 | last post: by

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.