Simply, an unbound method requires you to pass an object in for self
.
A bound method automatically passes the class instance as self
.
Also see this answer and take a look at descriptors for a better understanding of methods.
In code:
# class definition
>>> class Foo:
>>> def bar(self):
>>> print 'self:', self
# class instance -> bound method
>>> a = Foo()
>>> print a, a.bar
<__main__.Foo instance at 0x107bcda70> <bound method Foo.bar of <__main__.Foo instance at 0x107bcda70>>
>>> a.bar()
self: <__main__.Foo instance at 0x107bcda70>
# class -> unbound method
>>> print Foo, Foo.bar
__main__.Foo <unbound method Foo.bar>
>>> Foo.bar()
TypeError: unbound method bar() must be called with Foo instance as first argument (got nothing instead)
# manually binding a method
>>> b = Foo.bar.__get__(a, Foo)
>>> print b
<bound method Foo.bar of <__main__.Foo instance at 0x107bcda70>>
>>> b()
self: <__main__.Foo instance at 0x107bcda70>