Skip to content

Instantly share code, notes, and snippets.

@startling
Last active December 14, 2015 20:08
Show Gist options
  • Save startling/5141223 to your computer and use it in GitHub Desktop.
Save startling/5141223 to your computer and use it in GitHub Desktop.
Multiple-argument decorators in python via classes!
def multidecorator(fn):
class _MultiDecorator(object):
class __metaclass__(type):
def __new__(cls, name, bases, dict):
# No magic on the first class with this metaclass
if bases == (object,):
return type.__new__(cls, fn.__name__, bases, dict)
else:
return fn(**dict)
return _MultiDecorator
@multidecorator
def whatever(first, second, **_):
print first(1, 1)
print second(1, 1)
class something(whatever):
def first(a, b):
return a + b
def second(a, b):
return a * b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment