您能解释闭包(因为它们与Python有关)吗?

您能解释闭包(因为它们与Python有关)吗?,第1张

您能解释闭包(因为它们与Python有关)吗?

封闭封闭

对象是带有方法的数据,闭包是带有数据的函数。

def make_counter():    i = 0    def counter(): # counter() is a closure        nonlocal i        i += 1        return i    return counterc1 = make_counter()c2 = make_counter()print (c1(), c1(), c2(), c2())# -> 1 2 1 2


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5630810.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存