def sample():
n = 2018
def func():
print('n=', n)
def get_n():
return n
def set_n(value):
nonlocal n
n = value
# 暴露给外面访问内部元素。
func.get_n = get_n
func.set_n = set_n
return func
f = sample()
f()
f.set_n(2019)
print(f.get_n())
输出:
n= 2018
2019