Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #56025

Re: Parameterized functions of no arguments?

References <ij2fdr$uo7$1@news.eternal-september.org>
Date 2011-02-10 21:50 -0800
Subject Re: Parameterized functions of no arguments?
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.91.1297403413.1633.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Feb 10, 2011 at 8:54 PM, Rotwang <sg552@hotmail.co.uk> wrote:
> Hi all
>
> Sorry if this is a dumb question, I would guess it's answered in an FAQ
> somewhere but I haven't been able to find anything that helps. I'm trying to
> use Tkinter to create a menu whose entries and corresponding commands depend
> on some list x. I want the menu options to be labelled by the elements of x,
> and upon selecting an option k I want my program to execute a function f(k).
> So I tried writing something that schematically looked like this:
>
>    def f(k):
>        [do something that depends on k]
>
>    menu = Tkinter.Menu(master, tearoff = 0)
>    for k in x:
>        menu.add_command(label = str(k), command = lambda: f(k))
>
> The trouble is, whenever I open the menu and click on any entry k, instead
> of evaluating f(k) it always evaluates f(x[-1]). I've also tried this:
>
>    menu = Tkinter.Menu(master, tearoff = 0)
>    for k in x:
>        def f():
>            [do something that depends on k]
>        menu.add_command(label = str(k), command = f)
>
> and it gives me the same problem. Can anybody suggest a way around this?

It's a well-known problem due to the intricacies of Python's scoping rules.
One workaround is:

for k in x:
    def f(k=k):
[rest same as before]

Basically, the problem is that f() does close over the variable k, but
not the particular value of k at the time it was defined; f() looks up
the value of k anew each time it's called. But by the time you get
around to actually calling f(), the loop has terminated and thus k
retains its last value of x[-1]. Default argument values, however, are
evaluated only once and at definition-time, thus achieving the
intended behavior.

Cheers,
Chris
--
http://blog.rebertia.com

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Parameterized functions of no arguments? Rotwang <sg552@hotmail.co.uk> - 2011-02-11 04:54 +0000
  Re: Parameterized functions of no arguments? Hrvoje Niksic <hniksic@xemacs.org> - 2011-02-11 15:43 +0100
  Re: Parameterized functions of no arguments? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-02-10 23:22 -0800
  Re: Parameterized functions of no arguments? Carl Banks <pavlovevidence@gmail.com> - 2011-02-11 05:59 +0000
  Re: Parameterized functions of no arguments? Chris Rebert <clp2@rebertia.com> - 2011-02-10 21:50 -0800
  Re: Parameterized functions of no arguments? Rotwang <sg552@hotmail.co.uk> - 2011-02-11 05:26 +0000

csiph-web