__init__ is the initialiser

Ethan Furman ethan at stoneleaf.us
Fri Jan 31 15:41:42 EST 2014


On 01/31/2014 11:52 AM, Ned Batchelder wrote:
> On 1/31/14 2:33 PM, Mark Lawrence wrote:
>>  From http://docs.python.org/3/reference/datamodel.html#object.__init__
>> which states:-
>>
>> "
>> Called when the instance is created. The arguments are those passed to
>> the class constructor expression. If a base class has an __init__()
>> method, the derived class’s __init__() method, if any, must explicitly
>> call it to ensure proper initialization of the base class part of the
>> instance; for example: BaseClass.__init__(self, [args...]). As a special
>> constraint on constructors, no value may be returned; doing so will
>> cause a TypeError to be raised at runtime.
>> "
>>
>> Should the wording of the above be changed to clearly reflect that we
>> have an initialiser here and that __new__ is the constructor?
>>
>
> I'm torn about that.  The fact is, that for 95% of the reasons you want to say "constructor", the thing you're
> describing is __init__.  Most classes have __init__, only very very few have __new__.

My problem is with the first sentence, as it makes it sound like there is no __new__ and everything you need is in 
__init__.  Figuring out how __new__ and __init__ were tied together took a long time because of that.

> Why can't we call __init__ the constructor and __new__ the allocator?

I'm not so much concerned about the names (although I like those names ;) as I am about the prose.  If that first line 
was something like:

   Called after the instance has been created but before it is returned.

I think that would be much clearer.

--
~Ethan~



More information about the Python-list mailing list