如何在lambda函数中执行赋值

2024-05-15 07:57:32 发布

您现在位置:Python中文网/ 问答频道 /正文

Python2.7.1

我想知道为什么我不能做以下看起来是明智的事情

def do_stuff():
    # return a function which takes a map as an argument and puts a key in there
    f = lambda map: map['x'] = 'y' #compilation error
    return f 

x = do_stuff()
map = {}
x(map)
print map['x']

我可以让lambda函数变得更简单,比如f = lambda map: os.path.exists,但是我不能让它更改映射。有人能告诉我怎样才能做到这一点吗?如果这根本不可能为什么?


Tags: andlambdaanmapwhichreturndefas