Last active
August 29, 2015 14:14
-
-
Save jhowliu/64e023b21dd3d071cfde to your computer and use it in GitHub Desktop.
Decorator example
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
# Function Decorator | |
def Logged(f): | |
def wrapper(): | |
print f.__name__ | |
return f | |
# Class Decorator | |
class decorator(): | |
def __init__(self, f): | |
print "Initialzing decorator" | |
self.f = f | |
print "Exiting initiazation" | |
# Because of class is not callable by itself, we add __call__ to be callable. | |
def __call__(self, *args): | |
print "Entering call" | |
f = self.f(*args) | |
print "Exiting call" | |
return f | |
@Logged | |
def hola(): | |
print "Hola" | |
# Initailize decorator and Call decorator class | |
@decorator | |
def hello(): | |
print "Hello World" | |
if __name__ == '__main__': | |
print "Class Decorator Started" | |
hello() | |
print "\nFunction Decorator Stared" | |
hola() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Decorator example in python