[ python-Bugs-1390991 ] lambda functions confused when mapped in dictionary object

SourceForge.net noreply at sourceforge.net
Tue Dec 27 04:00:17 CET 2005


Bugs item #1390991, was opened at 2005-12-27 03:00
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1390991&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Samuel Hsiung (shsiung)
Assigned to: Nobody/Anonymous (nobody)
Summary: lambda functions confused when mapped in dictionary object

Initial Comment:
***Background***
Lambda functions wrapped around different module
functions overwrite each other when mapped one at a
time in a dictionary object.

Python Version: 2.4.2

***Duplication Instructions***

1. Setup

===module foo.py===
def callme(x):
    print "foo called!"
    print x

===module bar.py===
def callme(x):
    print "bar called!"
    print x

===module call.py===
import foo, bar

api = {}
modules = (foo, bar)

for module in modules:
    api[module] = lambda x: module.callme(x)
    print '%s mapped to %s' % (module, api[module])

api[foo]('above line should be foo')
api[bar]('above line should be bar')

print "foo lambda: %s" % api[foo]
print "bar lambda: %s" % api[bar]


2. Execution
=> python call.py

<module 'foo' from '/home/shsiung/playground/foo.pyc'>
mapped to <function <lambda> at 0xb7f7abc4>
<module 'bar' from '/home/shsiung/playground/bar.pyc'>
mapped to <function <lambda> at 0xb7f7abfc>
bar called!
above line should be foo
bar called!
above line should be bar
foo lambda: <function <lambda> at 0xb7f7abc4>
bar lambda: <function <lambda> at 0xb7f7abfc>

3. Expected behaviour
foo should have been called, followed by bar; instead
bar was called twice.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1390991&group_id=5470


More information about the Python-bugs-list mailing list