functors

David Eppstein eppstein at ics.uci.edu
Tue Jul 1 21:42:27 EDT 2003


In article <3jc4gvgn5rb4c4008eedg958ee594lu8p2 at 4ax.com>,
 Tom Plunket <tomas at fancy.org> wrote:

> How can I create a functor object in Python?
> 
> What I want (being a C++ coder <g>), is to be able to create an
> object that I is callable.  The following is my attempt, but it
> doesn't work:
...
> class SomeClass:
>     def __init__(self):
>         self.countdown = Countdown()
>         self.countdown.SetCallback(30, lambda s=self: s.Callback)

The error is in this line.  Should be s.Callback().
As it is, your lambda is a function that returns a function object, 
rather than calling it as you apparently intend.

>         # I have also tried 'lambda: self.Callback'
>         self.done = False

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list