Rotwang <sg...@hotmail.co.uk> writes: > menu = Tkinter.Menu(master, tearoff = 0) > for k in x: > def f(j = k): > [do something that depends on j] > menu.add_command(label = str(k), command = f) > > Still, I'd like to know if there's a more elegant method for creating > a set of functions indexed by an arbitrary list.
That is a standard python idiom. These days maybe I'd use partial evaluation: from functools import partial def f(k): whatever... for k in x: menu.add_command(label=str(k), command=partial(f, k)) the "pure" approach would be something like def f(k): whatever... for k in x: menu.add_command(label=str(k), command=(lambda x: lambda: f(x))(k)) -- http://mail.python.org/mailman/listinfo/python-list