Last active
September 10, 2015 12:35
-
-
Save varhub/dc1a5dec0a263e0a809e to your computer and use it in GitHub Desktop.
Example of how to use decorators
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
## Public Domain | |
# Victor Arribas <v.arribas.urjc@gmail.com> | |
def deco(fn): | |
def wrapper(*args, **kwargs): | |
print 'wrapped' | |
fn(*args, **kwargs) | |
return wrapper | |
@deco | |
def test(): | |
print 'test' | |
if __name__ == '__main__': | |
test() | |
class MyClass: | |
def deco(fn): | |
def wrapper(self, *args, **kwargs): | |
print 'wrapped(self)' | |
fn(self, *args, **kwargs) | |
return wrapper | |
@deco | |
def test(self, i): | |
print "test",i | |
if __name__ == '__main__': | |
c = MyClass() | |
c.test(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment