Re: functors

Discussion in 'Python' started by Erik Max Francis, Jul 2, 2003.

  1. 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.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
     
    Erik Max Francis, Jul 2, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul MG
    Replies:
    2
    Views:
    414
    Dhruv
    Jul 3, 2003
  2. Rob Williscroft

    Re: Need help with generic functors....

    Rob Williscroft, Aug 17, 2003, in forum: C++
    Replies:
    3
    Views:
    364
    Gordon Scott
    Aug 18, 2003
  3. red floyd
    Replies:
    0
    Views:
    350
    red floyd
    Nov 13, 2003
  4. nsgi_2004

    functors

    nsgi_2004, Aug 12, 2004, in forum: C++
    Replies:
    2
    Views:
    490
    Karl Heinz Buchegger
    Aug 12, 2004
  5. Satish
    Replies:
    1
    Views:
    817
    David Hilsee
    Sep 10, 2004
Loading...

Share This Page