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
,但是我不能让它更改映射。有人能告诉我怎样才能做到这一点吗?如果这根本不可能为什么?
不能在表达式中使用赋值,它是一个语句。
lambda
只能包含一个表达式,并且不包含语句。但是,您可以使用^{} function 来将分配给映射:
相关问题 更多 >
编程相关推荐