Questions tagged «lexical-closures»

10
Lambda函数的范围及其参数?
我需要一个与一系列gui事件几乎完全相同的回调函数。该函数的行为会有所不同,具体取决于调用该事件的事件。对我来说,这似乎是一个简单的案例,但是我无法弄清楚lambda函数的这种奇怪行为。 因此,我在下面有以下简化代码: def callback(msg): print msg #creating a list of function handles with an iterator funcList=[] for m in ('do', 're', 'mi'): funcList.append(lambda: callback(m)) for f in funcList: f() #create one at a time funcList=[] funcList.append(lambda: callback('do')) funcList.append(lambda: callback('re')) funcList.append(lambda: callback('mi')) for f in funcList: f() 此代码的输出是: mi mi mi …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.