Created
December 13, 2013 20:10
-
-
Save Artanis/7950536 to your computer and use it in GitHub Desktop.
Part of response to http://blog.endpoint.com/2013/12/python-decorator-basics.html
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
def debug(msg): | |
print("calling debug decorator") | |
def actual_decorator(fn): | |
print("calling actual_decorator") | |
def wrapper(*args): | |
print("calling wrapper with" + str(args)) | |
print(msg) | |
print("calling " + fn.__name__) | |
return fn(*args) | |
print("returning wrapper") | |
return wrapper | |
print("returning actual_decorator") | |
return actual_decorator | |
print("Decoration Time") | |
@debug("Let's multiply") | |
def mul(x, y): | |
return x * y | |
print("\nCall Time") | |
print(mul(5, 2)) | |
# ====== | |
# Output | |
# ====== | |
# Decoration Time | |
# calling debug decorator | |
# returning actual_decorator | |
# calling actual_decorator | |
# returning wrapper | |
# | |
# Call Time | |
# calling wrapper with (5, 2) | |
# Let's multiply | |
# calling mul | |
# 10 | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment