explicit call to __init__(self) in subclass needed?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Sep 18 10:41:07 CEST 2009


Ethan Furman a écrit :
> Andrew MacKeith wrote:
>> I create a class like this in Python-2.6
>>
>>  >>> class Y(str):
>> ...   def __init__(self, s):
>> ...      pass
>> ...
>>  >>> y = Y('giraffe')
>>  >>> y
>> 'giraffe'
>>  >>>
>>
>> How does the base class (str) get initialized with the value passed to 
>> Y.__init__() ?
>>
>> Is this behavior specific to the str type, or do base classes not need 
>> to be explicitly initialized?
>>
>> Andrew
> 
> All the immutable base types (I *think*), use __new__ for object 
> creation, not __init__.

All types use __new__ for object *creation*. __init__ is for object 
initialization (which indeed happens in the __new__ method for immutable 
types, since the initializer works my mutating the newly created 
instance) !-)

> ~Ethan~



More information about the Python-list mailing list