Skip to content

Instantly share code, notes, and snippets.

@terror
Created April 29, 2021 01:43
Show Gist options
  • Save terror/df4b620fa52e593a3f55e3db12d4140e to your computer and use it in GitHub Desktop.
Save terror/df4b620fa52e593a3f55e3db12d4140e to your computer and use it in GitHub Desktop.
# 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