What exactly are bound methods?

K

Kylotan

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?
 
E

Erik Max Francis

Kylotan said:
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:
.... def f(self, x):
.... print x
.... 1


--
Erik Max Francis && (e-mail address removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \
\__/ There are defeats more triumphant than victories.
-- Montaigne
 
S

Skip Montanaro

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/Cookbook/Python/Recipe/52549

Skip
 
M

Michele Simionato

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?

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 ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,171
Messages
2,570,936
Members
47,472
Latest member
KarissaBor

Latest Threads

Top