Skip to content

Instantly share code, notes, and snippets.

@ekozlowski
Created April 27, 2017 04:33
Show Gist options
  • Save ekozlowski/aa4e10165f75ba27fb8ef75b71573479 to your computer and use it in GitHub Desktop.
Save ekozlowski/aa4e10165f75ba27fb8ef75b71573479 to your computer and use it in GitHub Desktop.
demo of multiple levels of decorator unwrapping with __wrapped__ attribute
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__()
@ekozlowski
Copy link
Author

This functionality exists in Python 3.4+ (verified on 3.6), but does not exist on Python 2.x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment