Erik Max Francis
max at alcyone.com
Wed Jul 2 10:43:37 CEST 2003
Mirko Zeibig wrote:
> it's even simpler and maybe to obvious for a C++-afficinado:
> > class SomeClass:
> > def __init__(self):
> > self.countdown = Countdown()
> > self.countdown.SetCallback(30, lambda s=self: s.Callback)
> self.countdown.SetCallback(30, self.Callback)
> You don't need lambda here, just give the Countdown class a reference
> to your
> callback-method by specifying it's name *without* parentheses :-).
> If you print self.Callback, you will the information, that this is a
> instance-bound method object.
This feature of bound vs. unbound methods is one of the things that,
early on, made me really warm up to Python. The example you provide is
in fact the situation in which they're often the most useful: when
needing callbacks, usually for event-based systems (including GUIs).
C++ has member function pointers, but it doesn't have the standalone
concept of bound member functions, so functors are in fact usually used
for precisely this case, where you want to get a single callable object
that binds a member function pointer to an instance pointer/reference.
Python's builtin ability to usefully distinguish between bound and
unbound methods usually eliminates that need entirely; that is to say, a
bound method _is_ in fact a "functor," in that it's simply a callable
object that has the desired effect.
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ I'm trying to forget / But I can't act as if we never met
\__/ Chante Moore
More information about the Python-list