This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def function_decorator(f): | |
print('decorating {}'.format(f.__name__)) | |
def wrapper(*args, **kwargs): | |
print('calling {}'.format(f.__name__)) | |
return wrapper | |
@function_decorator | |
def decorated_function(): | |
print('actual call') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Demo of late binding in Python's closures and how to avoid it when needed | |
functions = [] | |
for n in [1, 2, 3]: | |
def func(x): | |
return n*x | |
functions.append(func) | |
# You would expect this to print [2, 4, 6] | |
print( |