Skip to content

Instantly share code, notes, and snippets.

@ylt6
Created March 20, 2017 06:11
Show Gist options
  • Save ylt6/2bf59a2f55ebed03f6abb3b18ea0a569 to your computer and use it in GitHub Desktop.
Save ylt6/2bf59a2f55ebed03f6abb3b18ea0a569 to your computer and use it in GitHub Desktop.
python decorator that accepts 0 or multiple arguments
def deco(*dargs):
def wrapper(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
for _ in range(dargs[0]):
print('has argument')
func(*args, **kwargs)
return wrapped
if len(dargs) > 1 or not callable(dargs[0]):
return wrapper
else:
@functools.wraps(dargs[0])
def wrapped(*args, **kwargs):
print('no argument')
dargs[0](*args, **kwargs)
return wrapped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment