Created
April 27, 2017 04:33
-
-
Save ekozlowski/aa4e10165f75ba27fb8ef75b71573479 to your computer and use it in GitHub Desktop.
demo of multiple levels of decorator unwrapping with __wrapped__ attribute
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
from functools import wraps | |
def w(func): | |
print("wrapping") | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
print("I'm inside the wrapper") | |
result = func(*args, **kwargs) | |
print("leaving the wrapper") | |
return result | |
return wrapper | |
@w | |
@w | |
@w | |
def foo(): | |
print("in foo") | |
if __name__ == "__main__": | |
foo() | |
foo.__wrapped__() | |
foo.__wrapped__.__wrapped__() | |
foo.__wrapped__.__wrapped__.__wrapped__() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This functionality exists in Python 3.4+ (verified on 3.6), but does not exist on Python 2.x.