检查闭包
您正在教您的侄女学习 Python 编程。她正在练习返回嵌套函数。她认为代码已经写对了,但担心返回的函数在调用时拿不到所需的信息。请向她展示:新函数的闭包中包含它所需的所有非局部变量。
本练习是课程的一部分
Python 函数编写
交互式实操练习
通过完成这段示例代码来试试这个练习。
def return_a_func(arg1, arg2):
def new_func():
print('arg1 was {}'.format(arg1))
print('arg2 was {}'.format(arg2))
return new_func
my_func = return_a_func(2, 17)
# Show that my_func()'s closure is not None
print(my_func.____ is not None)