Last active
September 20, 2016 16:49
-
-
Save xpostudio4/72fbae83f2a69dd8f69e to your computer and use it in GitHub Desktop.
Decorators Cheat Sheet
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
import functools # Part of Python standard library | |
def decorator(wrapped_function): | |
def _wrapper(*args, **kwargs): | |
# do something before the function call | |
result = wrapped_function(*args, **kwargs) | |
# do something after the function call | |
return result | |
return _wrapper | |
# decorator with functools.wraps added | |
def decorator_with_wraps(wrapped_function): | |
@functools.wraps(wrapped_function) | |
def _wrapper(*args, **kwargs): | |
# do something before the function call | |
result = wrapped_function(*args, **kwargs) | |
# do something after the function call | |
return result | |
return _wrapper | |
import wrapt # Requires installing the 'wrapt' library | |
# decorator powered by wrapt | |
@wrapt.decorator | |
def decorator_with_wrapt(wrapped_function, instance, args, kwargs): | |
# do something before the function call | |
result = wrapped_function(*args, **kwargs) | |
# do something after the function call | |
return result | |
def test_decorators(): | |
@decorator | |
def func1(): | |
return 'I' | |
@decorator_with_wraps | |
def func2(): | |
return 'code' | |
@decorator_with_wrapt | |
def func3(): | |
return 'python' | |
assert func1() == 'I' | |
assert func2() == 'code' | |
assert func3() == 'python' | |
#decorators that accept arguments | |
def arguments_decorator(arg1, arg2): | |
def _outer_wrapper(wrapped_function): | |
def _wrapper(*args, **kwargs): | |
# do something before the function call | |
result = wrapped_function(*args, **kwargs) | |
# do something after the function call | |
# Demonstrating what you can do with decorator arguments | |
result = result * arg1 * arg2 | |
return result | |
return _wrapper | |
return _outer_wrapper | |
def arguments_decorator_with_wraps(arg1, arg2): | |
def _outer_wrapper(wrapped_function): | |
@functools.wraps(wrapped_function) | |
def _wrapper(*args, **kwargs): | |
# do something before the function call | |
result = wrapped_function(*args, **kwargs) | |
# do something after the function call | |
# Demonstrating what you can do with decorator arguments | |
result = result * arg1 * arg2 | |
return result | |
return _wrapper | |
return _outer_wrapper | |
def arguments_decorator_with_wrapt(arg1, arg2): | |
@wrapt.decorator | |
def _wrapper(wrapped_function, instance, args, kwargs): | |
# do something before the function call | |
result = wrapped_function(*args, **kwargs) | |
# do something after the function call | |
# Demonstrating what you can do with decorator arguments | |
result = result * arg1 * arg2 | |
return result | |
return _wrapper | |
def test_arguments_decorators(): | |
@arguments_decorator(2, 3) | |
def func4(): | |
return 'We' | |
@arguments_decorator_with_wraps(2, 2) | |
def func5(): | |
return 'code' | |
@arguments_decorator_with_wrapt(3, 2) | |
def func6(): | |
return 'python' | |
assert func4() == 'WeWeWeWeWeWe' | |
assert func5() == 'codecodecodecode' | |
assert func6() == 'pythonpythonpythonpythonpythonpython' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to @pydanny