Checking for closure
You're teaching your niece how to program in Python, and she is working on returning nested functions. She thinks she has written the code correctly, but she is worried that the returned function won't have the necessary information when called. Show her that all of the nonlocal variables she needs are in the new function's closure.
This exercise is part of the course
Writing Functions in Python
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
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)