closure
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)
print(my_func.__closure__ is not None)
print(len(my_func.__closure__) == 2)
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)
print(my_func.__closure__ is not None)
print(len(my_func.__closure__) == 2)
closure_values = [
my_func.__closure__[i].cell_contents for i in range(2)
]
print(closure_values == [2, 17])
@print_args
def my_function(a, b, c):
print(a + b + c)
my_function(1, 2, 3)
def print_before_and_after(func):
def wrapper(*args):
print('Before {}'.format(func.__name__))
func(*args)
print('After {}'.format(func.__name__))
return wrapper
@print_before_and_after
def multiply(a, b):
print(a * b)
multiply(5, 10)