Skip to Content
Python Cookbook
book

Python Cookbook

by Alex Martelli, David Ascher
July 2002
Intermediate to advanced
608 pages
15h 46m
English
O'Reilly Media, Inc.
Content preview from Python Cookbook

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Modern Python Cookbook - Second Edition

Modern Python Cookbook - Second Edition

Steven F. Lott
Python Cookbook, 3rd Edition

Python Cookbook, 3rd Edition

David Beazley, Brian K. Jones

Publisher Resources

ISBN: 0596001673Supplemental ContentCatalog PageErrata