Skip to content

Instantly share code, notes, and snippets.

@SVilgelm
Last active May 26, 2017 18:03
Show Gist options
  • Save SVilgelm/e81f979a66506f2d1951 to your computer and use it in GitHub Desktop.
Save SVilgelm/e81f979a66506f2d1951 to your computer and use it in GitHub Desktop.
Python decorator
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__)
$ 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