callbacks in python

Larry Bates larry.bates at`
Wed Aug 13 23:07:52 CEST 2008

Alexandru Mosoi wrote:
> does anyone know a nice implementation of callbacks in python? i have
> issues mixing named & unamed parameters. i want build a callback over
> a function such that some parameters are passed when callback is
> created and the rest are passed when the function is called.
> example:
> callback = Callback(function, x=1, y)
> callback(z, t=4, u)

First problem is that all unnamed arguments must come BEFORE named ones (Python 
limitation).  To do what you want define you callback as a class instead.

class Callback(object):
     def __init__(x, y):
         self.x = x
         self.y = y

     def __call__(z, t, u):
         # Do what you want done at every callback

callback = Callback(x, y)

callback(z, t, u)


More information about the Python-list mailing list