Created
April 29, 2021 01:43
-
-
Save terror/df4b620fa52e593a3f55e3db12d4140e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# python functions are objects | |
# can be passed stored in variables, passed as args, returned, etc | |
def wrap(func): | |
print("wow!") | |
func() | |
print("oo") | |
def hello(): | |
print("hello!") | |
""" | |
A decorator is a function that allows us to wrap another function in order | |
to extend the behavior of the wrapped function, without permanently modifying it. | |
""" | |
def dec(func): | |
def wrap(): | |
print('Extended behaviour!') | |
func() | |
return wrap | |
@dec | |
def decoratee(): | |
print('hello!') | |
# also can do: decoratee = dec(decoratee) | |
if __name__ == '__main__': | |
# I. | |
x = wrap | |
x(hello) | |
# II. | |
decoratee() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment