Re: Python constructors have particular semantics, and ‘Foo.__init__’ doesn't qualify
Terry Reedy
tjreedy at udel.edu
Thu Dec 15 13:05:31 EST 2016
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.
>>> class C:
def __init__(self): pass
>>> ci = C().__init__
>>> ci.__self__
<__main__.C object at 0x000001E38A750E80>
> Because “Instance method objects have attributes, too: m.__self__ is the
> instance object with the method m() […]”.
This line is from section 9.7. Odds and Ends. In this quote, 'm' is a
method object, the result of 'instance.method, not a function. In
Python 2, 'm' would have been called a 'bound method', as opposed to an
unbound method. Since the latter were eliminated in 3.x, the adjective
is no longer needed.
--
Terry Jan Reedy
More information about the Python-list
mailing list