__init__ is the initialiser

Terry Reedy tjreedy at udel.edu
Fri Jan 31 21:40:32 EST 2014


On 1/31/2014 7:13 PM, Ethan Furman wrote:
> On 01/31/2014 03:47 PM, Ben Finney wrote:
>>
>> I would prefer it to be clear that “__init__” is called automatically,
>> *during* the constructor's operation. So, instead of:
>>
>>      Called when the instance is created.
>>
>> I suggest:
>>
>>      Called automatically by the constructor “__new__” during instance
>>      creation, to initialise the new instance.
>
> But __new__ does not call __init__, type does [1].

If the class is an instance of type, it is type.__call__,

 >>> tuple.__call__
<method-wrapper '__call__' of type object at 0x0000000067E8ED30>

I presume it is basically something like this:

class type:
   def __call__(self, *args, **kwds):
     instance = self.__new__(cls, *args, **kwds)
     self.__init__(instance, *args, **kwds)
     return instance

.__new__ and .__init__ are the plugins used by the true class instance 
constructor.

-- 
Terry Jan Reedy





More information about the Python-list mailing list