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


Groups > comp.lang.python > #33393

Re: Dictionary of Functions

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; '[1]:': 0.09; '[2]:': 0.09; '[3]:': 0.09; 'argument:': 0.09; 'def': 0.10; 'finished': 0.15; '[4]:': 0.16; 'called,': 0.16; 'complicated,': 0.16; 'dictionaries': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'lambda': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'wrote:': 0.17; 'parameters': 0.20; 'all,': 0.21; 'trying': 0.21; 'kevin': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'separate': 0.27; 'see,': 0.27; 'actual': 0.28; 'dictionary': 0.29; 'parameters.': 0.29; 'received:192.168.1.3': 0.29; 'function': 0.30; 'to:addr:python-list': 0.33; 'entry': 0.33; 'returning': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'usual': 0.37; 'subject:: ': 0.38; 'possible.': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'header:Reply-To:1': 0.68; 'reply- to:no real name:2**0': 0.72; "'for'": 0.84; 'reply- to:addr:python.org': 0.84; 'x):': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=TdYURGsh c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=1EKJ2blYo8kA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=I73uRbMjYtkA:10 a=jbB_4Bgm2Is1t_Gk4WEA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Thu, 15 Nov 2012 16:27:28 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20121026 Thunderbird/16.0.2
MIME-Version 1.0
To python-list@python.org
Subject Re: Dictionary of Functions
References <CAEUX0XfhsJTpnQifV6SoG3AxR=Kb=ORnjX_PLBx00M9CFKbZGw@mail.gmail.com>
In-Reply-To <CAEUX0XfhsJTpnQifV6SoG3AxR=Kb=ORnjX_PLBx00M9CFKbZGw@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
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.3718.1352996847.27098.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352996847 news.xs4all.nl 6852 [2001:888:2000:d::a6]:57467
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33393

Show key headers only | View raw


On 2012-11-15 16:04, Kevin Gullikson wrote:
> Hi all,
>
> I am trying to make a dictionary of functions, where each entry in the
> dictionary is the same function with a few of the parameters set to
> specific parameters. My actual use is pretty complicated, but I managed
> to boil down the issue I am having to the following example:
>
> In [1]: def test_fcn(a, x):
>     ...:     return a*x
>     ...:
>
> In [2]: fcn_dict = {}
>
> In [3]: for i in [1,2,3]:
>     ...:     fcn_dict[i] = lambda x: test_fcn(i, x)
>     ...:
>
> In [4]: fcn_dict
> Out[4]:
> {1: <function <lambda> at 0x102b42c08>,
>   2: <function <lambda> at 0x102b42b18>,
>   3: <function <lambda> at 0x102b42c80>}
>
> In [5]: fcn_dict[1](5)
> Out[5]: 15
>
> In [6]: fcn_dict[2](5)
> Out[6]: 15
>
> In [7]: fcn_dict[3](5)
> Out[7]: 15
>
>
> As you can see, all of the functions are returning the value that I want
> for fcn_dict[3]. If I make separate functions for each case instead of a
> dictionary it works, but I would really prefer to use dictionaries if
> possible. Is there a way to make this work?
>
It's looking up 'i' at the time that the function is called, which is
after the 'for' loop has finished and 'i' has been left as 3.

What you need to do is capture the current value of 'i'. The usual way
is with a default argument:

for i in [1,2,3]:
     fcn_dict[i] = lambda x, i=i: test_fcn(i, x)

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


Thread

Re: Dictionary of Functions MRAB <python@mrabarnett.plus.com> - 2012-11-15 16:27 +0000

csiph-web