Created
January 17, 2015 20:40
-
-
Save sujayy1983/89c60142cbeb3ed97ff4 to your computer and use it in GitHub Desktop.
A simple example of decorator.
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
""" | |
@Author: Sujayyendhiren Ramarao | |
@Description: Decorator design pattern | |
""" | |
def logging_decorator( function ): | |
def func(*args, **kwargs): | |
print("Entering function" + str(function)) | |
ret = function(*args,**kwargs) | |
print("Returned from function" + str(function)) | |
return ret | |
return func | |
class TestDecorator(): | |
def __init__(self): | |
self.value = True | |
@logging_decorator | |
def get(self): | |
return self.value | |
@logging_decorator | |
def set(self, value): | |
self.value = value | |
if __name__ == "__main__": | |
tDecor = TestDecorator() | |
print( "GET value: " + str( tDecor.get()) ) | |
print( "SET value: " + str( tDecor.set(False))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment