Skip to content

Instantly share code, notes, and snippets.

@simon-weber
Created April 3, 2014 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simon-weber/9956622 to your computer and use it in GitHub Desktop.
Save simon-weber/9956622 to your computer and use it in GitHub Desktop.
Use a paramaterized Python decorator without calling it.
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