Created
February 24, 2018 10:00
-
-
Save eagafonov/c769754dfe78ba43443fc9b7ad36a0e2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from functools import reduce, wraps | |
def dec1(func): | |
print("dec1: Decorating", func) | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
print("dec1: Calling {}(args={},kwargs={})".format(func.__name__, args, kwargs)) | |
return func(*args, **kwargs) | |
return wrapper | |
def dec2(func): | |
print("dec2: Decorating", func) | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
print("dec2: Calling {}(args={},kwargs={})".format(func.__name__, args, kwargs)) | |
return func(*args, **kwargs) | |
return wrapper | |
def dec3(func): | |
print("dec3: Decorating", func) | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
print("dec3: Calling {}(args={},kwargs={})".format(func.__name__, args, kwargs)) | |
return func(*args, **kwargs) | |
return wrapper | |
def apply_decorators(*decorators): | |
def wrapper(func): | |
return reduce(lambda func, decorator: decorator(func), decorators, func) | |
return wrapper | |
@apply_decorators(dec1, dec2, dec3) | |
def decorate_me_soflty(*args, **kwargs): | |
print("I'm the decorated one") | |
return 42 | |
print(decorate_me_soflty("Do it hard!")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment