Last active
May 26, 2017 18:03
-
-
Save SVilgelm/e81f979a66506f2d1951 to your computer and use it in GitHub Desktop.
Python decorator
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
import functools | |
def dec(func=None): | |
if func is None: | |
return lambda f: dec(f) | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
print('wrapper') | |
return func(*args, **kwargs) | |
return wrapper | |
def foo1(): | |
print('bar1') | |
@dec | |
def foo2(): | |
print('bar2') | |
@dec() | |
def foo3(): | |
print('bar3') | |
foo1() | |
foo2() | |
foo3() | |
print(foo1.__name__) | |
print(foo2.__name__) | |
print(foo3.__name__) |
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
$ python decorator.py | |
bar1 | |
wrapper | |
bar2 | |
wrapper | |
bar3 | |
foo1 | |
foo2 | |
foo3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment