Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2005, 07:00 AM
Kylotan
Guest
 
Posts: n/a
Default What exactly are bound methods?

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
  #2  
Old July 18th, 2005, 07:00 AM
Erik Max Francis
Guest
 
Posts: n/a
Default 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
  #3  
Old July 18th, 2005, 07:00 AM
Skip Montanaro
Guest
 
Posts: n/a
Default 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

  #4  
Old July 18th, 2005, 07:03 AM
Michele Simionato
Guest
 
Posts: n/a
Default 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]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.