Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # For debugging, the stacktrace prints you the function __name__
- def foo():
- print("foo")
- print(foo.__name__)
- #outputs: foo
- # With a decorator, it gets messy
- def bar(func):
- def wrapper():
- print("bar")
- return func()
- return wrapper
- @bar
- def foo():
- print("foo")
- print(foo.__name__)
- #outputs: wrapper
- # "functools" can help for that
- import functools
- def bar(func):
- # We say that "wrapper", is wrapping "func"
- # and the magic begins
- @functools.wraps(func)
- def wrapper():
- print("bar")
- return func()
- return wrapper
- @bar
- def foo():
- print("foo")
- print(foo.__name__)
- #outputs: foo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement