Created
April 3, 2014 15:31
-
-
Save simon-weber/9956622 to your computer and use it in GitHub Desktop.
Use a paramaterized Python decorator without calling it.
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 dual_decorator(func): | |
"""This is a decorator that converts a paramaterized decorator for no-param use.""" | |
# modified from http://stackoverflow.com/a/10288927/1231454. | |
@functools.wraps(func) | |
def inner(*args, **kw): | |
if ((len(args) == 1 and not kw and callable(args[0]) | |
# Exceptions are callable; the next line allows us to accept them as args. | |
and not (type(args[0]) == type and issubclass(args[0], BaseException)))): | |
return func()(args[0]) | |
else: | |
return func(*args, **kw) | |
return inner |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment