Skip to content

Instantly share code, notes, and snippets.

@jhowliu
Last active August 29, 2015 14:14
Show Gist options
  • Save jhowliu/64e023b21dd3d071cfde to your computer and use it in GitHub Desktop.
Save jhowliu/64e023b21dd3d071cfde to your computer and use it in GitHub Desktop.
Decorator example
# 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()
@jhowliu
Copy link
Author

jhowliu commented Feb 3, 2015

Decorator example in python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment