def outer(name):
def inner(name):
# name = "Mr.Zhang" #这里的name值会覆盖调用outer函数实参的值,这是涉及变量在函数作用域的范围
print("sub method :%s" %name)
# 使用__closuer__内置属性来判断是否是一个闭包,返回地址,则是,返回None,则不是闭包函数
print(inner.__closure__)
inner(name) #在内部直接调用
outer("GuiDo")
运行结果:
sub method :GuiDo
(<cell at 0x000001D2792EF7C8: function object at 0x000001D2793687B8>,)
e.g
2.返回函数名称
# -*- coding: utf-8 -*-
def sumer(num = 0):
count = [num]
def add():
count[0] += 1
return count[0]
return add
f = sumer(3)
print(f())
print(f())
print(f())
运行结果:
7
8
9