Created
December 9, 2015 06:43
-
-
Save 00krishna/fa12876e05e451cabe50 to your computer and use it in GitHub Desktop.
create an optional decorator
This file contains hidden or 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
class OptionalDecoratorDecorator(object): | |
def __init__(self, decorator): | |
self.deco = decorator | |
def __call__(self, func): | |
self.deco = self.deco(func) | |
self.func = func | |
def wrapped(*args, **kwargs): | |
if kwargs.get("no_deco") is True: | |
return self.func() | |
else: | |
return self.deco() | |
return wrapped | |
def spammer(func): | |
def wrapped(): | |
print "spam" | |
return func() | |
return wrapped | |
@OptionalDecoratorDecorator(spammer) | |
def test(): | |
print "foo" | |
test() | |
print "***" | |
test(no_deco=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment