I am trying to verify the difference between instance attributes and class attributes as laid out by the Python tutorial release 2.7.3 dated Nov 01, 2012, chapter 9: Classes, Page 66 last line (source):
Valid method names of an instance object depend on its class. By
definition, all attributes of a class that are function objects define
corresponding methods of its instances. So in our example, x.f is a
valid method reference, since MyClass.f is a function, but x.i is not,
since MyClass.i is not. But x.f is not the same thing as MyClass.f —
it is a method object, not a function object.
I have this:
class MyClass:
"""A simple example class"""
i = 12345
def f():
return 'hello world'
Then I do this:
>>> x = MyClass()
>>> x.f
<bound method MyClass.f of <__main__.MyClass instance at 0x02BB8968>>
>>> MyClass.f
<unbound method MyClass.f>
>>> type(MyClass.f)
<type 'instancemethod'>
>>> type(x.f)
<type 'instancemethod'>
Note that the type of both x.f and MyClass.f is instancemethod. There is no difference in types but the tutorial says otherwise. Can someone please clarify?
Bound vs Unbound Methods – an explanation.
… or why Python has the behaviour you point out.
So, first off, a note that this is different in 3.x. In 3.x, you will get
MyClass.fbeing a function, andx.fas a method – as expected. This behaviour is essentially a poor design decision that has later been changed.The reason for this is that Python has the concept of a method that is different to most languages, which is essentially a function with the first argument pre-filled as the instance (
self). This pre-filling makes a bound method.In Python 2.x and before, it was reasoned that a method not attached to an instance would be an unbound method, which was a function with the restriction that the first argument (
self), must be an instance of the object. This is then ready to be bound to an instance and become a bound method.With time, it became clear an unbound method is really just a function with this odd restriction that didn’t really matter (that
selfmust be of the ‘correct’ type), so they were removed from the language (in 3.x). This is essentially duck-typingself, which suits the language.Further reading.
This is a (condensed, from memory) explanation which can be read in full from Python creator Guido van Rossum’s own mouth in his ‘History of Python’ series.