Created
December 15, 2013 13:44
-
-
Save anupamsharma/7973234 to your computer and use it in GitHub Desktop.
Python decorator Examples with arguments example using Function
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
#Python decorator Example with arguments example using Function | |
def ed(arg1): | |
if arg1 == True: | |
print "decide1" | |
def real_d(f): | |
def rfunc(*args, **kwargs): | |
print "1", arg1 | |
print args | |
print kwargs | |
f(args, kwargs) | |
rfunc.__name__ = f.__name__ | |
return rfunc | |
return real_d | |
if arg1 == False: | |
print "decide2" | |
def real_d(f): | |
def rfunc(*args, **kwargs): | |
print "2", arg1 | |
print args | |
print kwargs | |
f(args, kwargs) | |
rfunc.__name__ = f.__name__ | |
return rfunc | |
return real_d | |
@ed(False) | |
def ef1(a, b): | |
print "I am in ef1", a, b | |
@ed(True) | |
def ef2(a, b): | |
print "I am in ef2", a, b | |
ef1(44, 45) | |
ef2(44, 45) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment