嵌套范围和Lambda [英] Nested Scopes and Lambdas

查看:105
本文介绍了嵌套范围和Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def funct():
    x = 4
    action = (lambda n: x ** n)
    return action

x = funct()
print(x(2)) # prints 16

...我不太明白为什么2被自动分配给n?

... I don't quite understand why 2 is assigned to n automatically?

推荐答案

nfunct返回的匿名函数的参数.完全等同的funct定义是

n is the argument of the anonymous function returned by funct. An exactly equivalent defintion of funct is

def funct():
    x = 4
    def action(n):
        return x ** n
    return action

这种形式还有其他意义吗?

Does this form make any more sense?

这篇关于嵌套范围和Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆