Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: functors

Reply
Thread Tools

Re: functors

 
 
Erik Max Francis
Guest
Posts: n/a
 
      07-02-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 && http://www.velocityreviews.com/forums/(E-Mail Removed) && 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting a map of pointers with functors Satish C++ 1 09-10-2004 01:40 AM
functors nsgi_2004 C++ 2 08-12-2004 07:35 AM
functors with reference parameters red floyd C++ 0 11-13-2003 12:50 AM
Re: Need help with generic functors.... Rob Williscroft C++ 3 08-17-2003 11:57 PM
Functors with behaviour in the destructor Paul MG C++ 2 07-03-2003 03:12 AM



Advertisments