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


Groups > comp.lang.python > #56025

Re: Parameterized functions of no arguments?

Path csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'behavior.': 0.07; 'terminated': 0.07; 'called.': 0.09; 'labelled': 0.09; 'lambda:': 0.09; 'values,': 0.09; 'somewhere': 0.10; 'this:': 0.10; 'pm,': 0.12; 'loop': 0.13; 'wrote:': 0.14; 'def': 0.14; 'argument': 0.15; 'command': 0.15; 'defined;': 0.16; 'evaluates': 0.16; 'f()': 0.16; 'tkinter': 0.16; 'workaround': 0.16; 'feb': 0.16; 'cc:no real name:2**0': 0.17; 'cheers,': 0.17; 'variable': 0.18; 'suggest': 0.19; 'this?': 0.20; 'cc:2**0': 0.21; 'guess': 0.21; 'tried': 0.22; 'trying': 0.22; 'thus': 0.22; 'received:209.85.214.174': 0.23; 'received:mail-iw0-f174.google.com': 0.23; 'rules.': 0.23; 'header:In-Reply-To:1': 0.23; 'cc:addr:python-list': 0.24; 'entries': 0.24; "python's": 0.24; 'looked': 0.25; "haven't": 0.26; 'function': 0.27; 'calling': 0.27; 'url:blog': 0.27; "i'm": 0.28; 'instead': 0.28; 'problem': 0.28; 'answered': 0.28; 'commands': 0.28; 'menu': 0.28; 'received:209.85.214': 0.28; 'message-id:@mail.gmail.com': 0.29; 'subject:?': 0.29; 'problem.': 0.29; 'basically,': 0.30; 'selecting': 0.30; 'cc:addr:python.org': 0.30; 'list': 0.31; "i've": 0.31; 'actually': 0.31; 'execute': 0.31; 'question,': 0.31; 'thu,': 0.31; 'however,': 0.32; 'option': 0.33; 'depends': 0.33; 'does': 0.33; 'looks': 0.34; 'options': 0.34; 'depend': 0.34; 'faq': 0.34; 'trouble': 0.34; '10,': 0.35; 'but': 0.36; 'anything': 0.36; 'is:': 0.36; 'open': 0.36; 'sorry': 0.36; 'subject:skip:P 10': 0.37; 'close': 0.37; 'received:209.85': 0.38; 'received:google.com': 0.38; 'skip:t 20': 0.38; 'particular': 0.38; 'subject:: ': 0.39; 'able': 0.39; 'some': 0.39; 'entry': 0.39; 'around': 0.40; "it's": 0.40; 'due': 0.69; 'click': 0.78; 'retains': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=4g7UuzZ+/6Na438AeCpoNhAh2Tv67CFQZvebLryryJc=; b=cgLc3m8Wc1aEb6f5WpR3ALSvqUM3xrOSPjA7xZGuaLI1OMOP3CuVtgdtDfLLfKQ6ya GxXjLXjc1AtPVR8rxW5cka+AOEBOfaW3LvvyQ2CEMVa9IXHhYbiJAPgamjO43Qdi2Ib3 dwKwNaNy2rriDVMDeWOfH4OtQOtdFTOr/1yKE=
DomainKey-Signature a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=OafJVMG1ZDEP/GhXcF3hoLtRAQ72IphDvQpzOYMDR4aE+mOIkQtqFTCKIQ3nOE+BQE 7mxlvdL5YugqYJ1fD3Ei085I+hzZq3HAWuZA6I3mlQuXXqAA39Saeld5rPT098DZ/8Fw 0Eyo1d9d0T9WL8G5IGmeung7Ev66l4C60S05A=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <ij2fdr$uo7$1@news.eternal-september.org>
References <ij2fdr$uo7$1@news.eternal-september.org>
Date Thu, 10 Feb 2011 21:50:10 -0800
X-Google-Sender-Auth 5Hfie1kqSPkSgUEWY5LELeK4h8M
Subject Re: Parameterized functions of no arguments?
From Chris Rebert <clp2@rebertia.com>
To Rotwang <sg552@hotmail.co.uk>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.91.1297403413.1633.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 82.94.164.166
X-Trace 1297403413 news.xs4all.nl 32470 [::ffff:82.94.164.166]:49402
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:56025

Show key headers only | 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