What exactly are bound methods?
Erik Max Francis
max at alcyone.com
Sun Nov 23 22:33:22 EST 2003
Kylotan wrote:
>
> Although I see lots of references to them in various documentation, I
> can't find a decent explanation of exactly what they are. I'm guessing
> that it's a reference to a method that remembers which object it came
> from, and that when it's called, it passes that object as the first
> parameter (which would conventionally be 'self'). Is this correct?
Yep:
>>> class C:
... def f(self, x):
... print x
...
>>> c = C() # c is an instance of C
>>> C
<class __main__.C at 0x402e141c>
>>> C.f # the unbound method
<unbound method C.f>
>>> c.f # the bound method
<bound method C.f of <__main__.C instance at 0x402e4c8c>>
>>> C.f(c, 1) # invoking unbound methods requires passing the instance
1
>>> c.f(1) # bound methods already contain the instance
1
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \
\__/ There are defeats more triumphant than victories.
-- Montaigne
More information about the Python-list
mailing list