Re: Python constructors have particular semantics, and ‘Foo.__init__’ doesn't qualify

Terry Reedy tjreedy at udel.edu
Thu Dec 15 16:09:46 EST 2016


On 12/15/2016 2:04 PM, Ian Kelly wrote:
> On Thu, Dec 15, 2016 at 11:05 AM, Terry Reedy <tjreedy at udel.edu> wrote:
>> On 12/14/2016 11:14 PM, Thomas 'PointedEars' Lahn wrote:
>>
>>> According to
>>> <https://docs.python.org/3/tutorial/classes.html#method-objects>,
>>> “Foo.__init__” is _not_ an instance method.  Were it an instance
>>> method, the following would not happen:
>>
>>
>> This link points to subsection 9.3.4. Method Objects
>>
>>> | >>> class Foo:
>>> | ...     def __init__ (self):
>>> | ...         pass
>>> | ...
>>> | >>> Foo.__init__.__self__
>>> | Traceback (most recent call last):
>>> |   File "<stdin>", line 1, in <module>
>>> | AttributeError: 'function' object has no attribute '__self__'
>>
>>
>> You have misread the docs.  Foo.__init__ is the function. Foo().__init__ is
>> a method object with the attribute __self__.  You omitted the ()s.
>
> I think this is actually the point that Thomas was making. He was
> responding to the assertion that "Foo.__init__" is an instance method
> and demonstrating that it's false because Foo is the class, not an
> instance of Foo.

The __init__ function *is* an 'instance method', but not a (bound) 
method object.  The distinction is important.  I admit that 'instance 
method' can be confusing if one does not understand the contextual 
meaning of the two words.  A 'method' is a function that is a class 
attribute. Being a method normally adds special behavior to the 
function.  An 'instance method', the default status of a method,  takes 
an *instance of the class* (or subclass) as first parameter.  A 'class 
method' (relatively rare) takes the *class* (or subclass) as first 
parameter.   A 'static method' (very rare) takes neither as first 
parameter and is really just a function.

-- 
Terry Jan Reedy




More information about the Python-list mailing list