Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 1.28 KB

decorators.md

File metadata and controls

31 lines (23 loc) · 1.28 KB

Python decorators (the @things before functions)

Python decorators are things like @decorator_name that you sometimes see before function definitions.

They can be really useful for things like:

They are quite a thing to get your head around at first, but if you remember that a decorator is "a function that you pass a function to, that then returns another function" along with the following example, then you have most of the mental model already:

# This code...
@my_decorator
def my_function():
   print("hello")

# is EXACTLY the same as this code...
def my_function():
   print("hello")
my_function = my_decorator(my_function)

# (my_decorator replaces the my_function variable with a modified function)

For some more examples, see: