Skip to content

Instantly share code, notes, and snippets.

@runningzyp
Created March 10, 2022 01:55
Show Gist options
  • Save runningzyp/c25a45f7866696db7fd810cf2a2c586c to your computer and use it in GitHub Desktop.
Save runningzyp/c25a45f7866696db7fd810cf2a2c586c to your computer and use it in GitHub Desktop.
decorator wrap order && decorator execution order
from functools import wraps
def decorator1(func):
print("start wrap1")
@wraps(func)
def wrapper(*args, **kwargs):
print("in decorator1")
result = func(*args, **kwargs)
print("return decorator1")
return result
print("wrap1 finish")
return wrapper
def decorator2(func):
print("start wrap2")
@wraps(func)
def wrapper(*args, **kwargs):
print("in decorator2")
result = func(*args, **kwargs)
print("return decorator2")
return result
print("wrap2 finish")
return wrapper
def decorator3(func):
print("start wrap3")
@wraps(func)
def wrapper(*args, **kwargs):
print("in decorator3")
result = func(*args, **kwargs)
print("return decorator3")
return result
print("wrap3 finish")
return wrapper
@decorator1
@decorator2
@decorator3
def addNum(x, y):
return x+y
addNum(1,2)
#
# print(addNum.__wrapped__(1, 2))
# print(addNum.__wrapped__.__wrapped__(1, 2))
# print(addNum.__wrapped__.__wrapped__.__wrapped__(1, 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment