Skip to content

Instantly share code, notes, and snippets.

@alik472
Last active April 6, 2018 12:44
Show Gist options
  • Save alik472/f2685482d33a910b7808d541c74bc759 to your computer and use it in GitHub Desktop.
Save alik472/f2685482d33a910b7808d541c74bc759 to your computer and use it in GitHub Desktop.
Python Decorators
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