Avoiding lambda in Writing Callback Functions
Credit: Danny Yoo
Problem
You need to use many callbacks without
arguments, typically while writing a Tkinter-based GUI, and would
rather avoid using lambda.
Solution
Between the classic lambda approach and a powerful
general-purpose currying mechanism, there’s a third,
extremely simple way that can come in handy in many practical cases:
class Command:
def _ _init_ _(self, callback, *args, **kwargs):
self.callback = callback
self.args = args
self.kwargs = kwargs
def _ _call_ _(self):
return apply(self.callback, self.args, self.kwargs)Discussion
I remember seeing this utility class a while back, but don’t remember who to attribute it to. Perhaps I saw this in John E. Grayson’s book, Python and Tkinter Programming (Manning).
Writing a lot of callbacks that give customized arguments can look a
little awkward with lambda, so this
Command class gives an alternative syntax that
looks nicer. For example:
import Tkinter def hello(name): print "Hello", name root = Tk( ) # the lambda way of doing it: Button(root, text="Guido", command=lambda name="Guido": hello(name)).pack( ) # using the Command class: Button(root, text="Guido", command=Command(hello, "Guido")).pack( )
Of course, you can also use a more general currying approach, which lets you fix some of the arguments when you bind the callback, while others may be given at call time (see Recipe 15.8). However, “doing the simplest thing that can possibly work” is a good programming principle. If ...