Skip to content

Instantly share code, notes, and snippets.

@deemson
deemson / decorators.py
Last active August 29, 2015 14:06
Demo of decorators usage in Python
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')
@deemson
deemson / late_binding.py
Created June 18, 2014 13:02
Demo of late binding in Python's closures and how to avoid it when needed
# 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(