functors

Mirko Zeibig mirko-lists at zeibig.net
Wed Jul 2 04:28:44 EDT 2003


On Wed, 02 Jul 2003 at 01:25 GMT, Tom Plunket <tomas at fancy.org> wrote:
Hello Tom,

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.

You can easily assign a new name to a function and call it afterwards:

li = []
al = li.append
al("Hello")
al("World")
print "".join(li)

This may even be faster as dot-resolution takes some time.

Regards
Mirko




More information about the Python-list mailing list