[Python-Dev] Proper initialization of structs
Nick Coghlan
ncoghlan at gmail.com
Thu Oct 30 23:36:25 CET 2008
Alexandre Vassalotti wrote:
> On Thu, Oct 30, 2008 at 1:00 PM, Fred Drake <fdrake at acm.org> wrote:
>> It's good to move work into __init__ where reasonable, so that it can be
>> avoided if a subclass wants it done in a completely different way, but new
>> can't work that way.
>>
>
> And that is exactly the reason why, the _pickle module doesn't use
> __new__ for initialization. Doing any kind of argument parsing in
> __new__ prevents subclasses from customizing the arguments for their
> __init__.
No it doesn't - it just means the subclasses have to override __new__ as
well and then give the parent class the arguments it needs.
I've used this convention (*must* call parent class __new__ or the
instance will be broken, may call parent class __init__ if it is
helpful) many times, and it is far more robust than relying on
subclasses to remember to call the parent class __init__ when setting up
the class.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
More information about the Python-Dev
mailing list