Skip to content

Instantly share code, notes, and snippets.

@maddrum
Created December 6, 2021 18:33
Show Gist options
  • Save maddrum/eec02a637969eb9a49c9b0539fb78524 to your computer and use it in GitHub Desktop.
Save maddrum/eec02a637969eb9a49c9b0539fb78524 to your computer and use it in GitHub Desktop.
def decorator(func):
"""This is a decorator function. It takes some function as a parameter, 'wraps it' in code
and RETURNs wrapped function
This is a way to add functionality without changing base code"""
def wrap(text='this is base function'):
print('this is pre-function')
func(text)
print('this is post func')
return wrap # NB! must return wrapper function. Must return function!!!
def base_function(text='this is base function'):
print(text)
@decorator
def decorated_func(text='this is base function'):
print(text)
# writing same line from before without decorator
new_decorated_function = decorator(base_function)
base_function()
print('----')
decorated_func()
print('----')
decorated_func(text='aaaa')
print('----')
# decorated function call without decorator
new_decorated_function()
print('----')
new_decorated_function(text='bbb')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment