Created
March 21, 2016 16:13
-
-
Save f3rdy/4f210c400990bda2d548 to your computer and use it in GitHub Desktop.
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
from functools import wraps | |
def hello(func): | |
@wraps(func) # fetch attributes from the original function | |
def proxy(*args, **kwargs): | |
print("Decorator Action") | |
return func(*args, **kwargs) | |
return proxy | |
@hello | |
def add(a, b): | |
return a + b |
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
def say(text): | |
def _say(func): | |
def __say(*args, **kwargs): | |
print(text) | |
return func(*args, **kwargs) | |
return __say | |
return _say | |
@say('hello') | |
def sub(a,b): | |
return a-b |
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
class C(): | |
@staticmethod # This is a decorator necessary that func() can be called | |
def func(): | |
print('Hallo') | |
c = C() | |
c.func() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment