Does __init__ of subclass need the same argument types as __init__ of base class?

Hrvoje Niksic hniksic at xemacs.org
Wed Mar 25 10:08:09 EDT 2009


Bruno Desthuilliers <bruno.42.desthuilliers at websiteburo.invalid>
writes:

>> The print command inside the __init__ method isn't executed, so that
>> method doesn't seem to start at all.
>
> this often happens with (usually C-coded) immutable types. The
> initializer is not called, only the "proper" constructor (__new__).

More specifically, __init__ is called, but only after __new__ has
finished, and __new__ gets the arguments passed in the constructor
expression.  Since __new__ raises an exception for those arguments,
__init__ is never reached.

The fix is in such cases is, as you point out, to override __new__ and
not bother with __init__ at all.



More information about the Python-list mailing list