Last active
December 14, 2015 20:08
-
-
Save startling/5141223 to your computer and use it in GitHub Desktop.
Multiple-argument decorators in python via classes!
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 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