Created
March 20, 2017 06:11
-
-
Save ylt6/2bf59a2f55ebed03f6abb3b18ea0a569 to your computer and use it in GitHub Desktop.
python decorator that accepts 0 or multiple arguments
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
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