嵌套作用域和 Lambda
def funct():
x = 4
action = (lambda n: x ** n)
return action
x = funct()
print(x(2)) # prints 16
...我不太明白为什么2会自动分配给n?
def funct():
x = 4
action = (lambda n: x ** n)
return action
x = funct()
print(x(2)) # prints 16
... I don't quite understand why 2 is assigned to n automatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
n
是funct
返回的匿名函数的参数。funct
的一个完全等价的定义是这种形式更有意义吗?
n
is the argument of the anonymous function returned byfunct
. An exactly equivalent defintion offunct
isDoes this form make any more sense?
它不是“自动”分配的:它是通过将其作为与
n
参数相对应的实际参数传递来非常明确且非自动分配的。设置 x 的复杂方法几乎与 def x(n): return 4**n 相同(除去 x.__name__ 和其他次要的内省细节) 。It's not assigned "automatically": it's assigned very explicitly and non-automatically by your passing it as the actual argument corresponding to the
n
parameter. That complicated way to setx
is almost identical (net ofx.__name__
and other minor introspective details) todef x(n): return 4**n
.