Modify arguments between __new__ and __init__

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Dec 23 07:01:19 EST 2007


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



More information about the Python-list mailing list