Created
December 6, 2021 18:33
-
-
Save maddrum/eec02a637969eb9a49c9b0539fb78524 to your computer and use it in GitHub Desktop.
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 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