Last active
April 6, 2018 12:44
-
-
Save alik472/f2685482d33a910b7808d541c74bc759 to your computer and use it in GitHub Desktop.
Python 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
def mydec(func): | |
def wrapper(*args,**kwargs): | |
print('before') | |
result=func(*args,**kwargs) | |
print('after') | |
return result | |
return wrapper | |
@mydec | |
def myfunc(name='Ali'): | |
print(name) | |
# Example 2: ================================== | |
def cough_dec(func): | |
def func_wrapper(): | |
# code before function | |
print(' *cough* ') | |
func() | |
#code after function | |
print(' *cough* ') | |
return func_wrapper | |
@cough_dec | |
def question(): | |
print('can you give me a discount on that?') | |
@cough_dec | |
def answer(): | |
print("it's only 50p you cheapskate") | |
# question = cough_dec(question) | |
question() | |
answer() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment