Connecting Tech Pros Worldwide Help | Site Map

What exactly are bound methods?

Kylotan
Guest
 
Posts: n/a
#1: Jul 18 '05
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?

--
Ben Sizer
Erik Max Francis
Guest
 
Posts: n/a
#2: Jul 18 '05

re: What exactly are bound methods?


Kylotan wrote:[color=blue]
>
> 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?[/color]

Yep:
[color=blue][color=green][color=darkred]
>>> class C:[/color][/color][/color]
.... def f(self, x):
.... print x
....[color=blue][color=green][color=darkred]
>>> c = C() # c is an instance of C
>>> C[/color][/color][/color]
<class __main__.C at 0x402e141c>[color=blue][color=green][color=darkred]
>>> C.f # the unbound method[/color][/color][/color]
<unbound method C.f>[color=blue][color=green][color=darkred]
>>> c.f # the bound method[/color][/color][/color]
<bound method C.f of <__main__.C instance at 0x402e4c8c>>[color=blue][color=green][color=darkred]
>>> C.f(c, 1) # invoking unbound methods requires passing the instance[/color][/color][/color]
1[color=blue][color=green][color=darkred]
>>> c.f(1) # bound methods already contain the instance[/color][/color][/color]
1


--
Erik Max Francis && max@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
Skip Montanaro
Guest
 
Posts: n/a
#3: Jul 18 '05

re: What exactly are bound methods?



Ben> Although I see lots of references to them in various documentation,
Ben> I can't find a decent explanation of exactly what they are. I'm
Ben> guessing that it's a reference to a method that remembers which
Ben> object it came from, and that when it's called, it passes that
Ben> object as the first parameter (which would conventionally be
Ben> 'self'). Is this correct?

When you define a class like so:

class foo:
def bar(self):
pass

foo.bar is an unbound method. Binding the method associates it with a
particular instance. You can think of a bound method as currying the
instance object with the unbound method. For info on currying, check the
Python Cookbook:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/52549

Skip

Michele Simionato
Guest
 
Posts: n/a
#4: Jul 18 '05

re: What exactly are bound methods?


kylotan@hotmail.com (Kylotan) wrote in message news:<153fa67.0311231859.551b673@posting.google.co m>...[color=blue]
> 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?[/color]

If you really want to learn the difference between functions, methods
and bound methods, you must learn descriptors:

http://users.rcn.com/python/download/Descriptor.htm

Warning: the study of descriptors may cause your head to explode ...
[color=blue][color=green][color=darkred]
>>> def f(self): pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> class C(object): pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> c=C()
>>> f.__get__(c,C)[/color][/color][/color]
<bound method C.f of <__main__.C object at 0x017716D0>>[color=blue][color=green][color=darkred]
>>> f.__get__(None,C)[/color][/color][/color]
<unbound method C.f>[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
Closed Thread