[Go to site: main page, start]

Decorators

Decorators are functions that accept functions and return functions. They're weird but powerful.

14 articles · 52 min read
14 screencasts · 48 min watch
01
The meaning of "callable"
3 min read Screencast available

A callable is a function-like object, meaning it's something that behaves like a function. The primary types of callables in Python are functions and classes, though other callable objects do exist.

Read
Read
02
Passing functions as arguments to other functions
3 min read Screencast available

In Python, you can pass functions (as an argument) to another function. Some of Python's built-in functions actually expect functions to be given as one or more of their arguments to call them later.

Read
Read
03
Nested functions
4 min read Screencast available

Functions in Python can be defined within another function.

Read
Read
04
What is a decorator?
5 min read Screencast available

A decorator is a callable (usually a function though sometimes a class) that accepts either a function or a class and returns a new function or class that wraps around the original one.

Read
Read
05
How to make a decorator
5 min read Screencast available

A decorator is a function that accepts a function and returns a function. That's true for function decorators at least.

Read
Read
06
Common decorators included with Python
2 min read Screencast available Premium

Python has a wide range of built-in decorators that turn complex logic into clean abstractions, like caching function calls, reducing redundancy, and customizing class behavior.

Read
Read
07
Making a well-behaved decorator
4 min read Screencast available Premium

I highly recommend all decorator functions use functools.wrap to help the decorator's replacement function pretend it's the original decorated function. Well-behaved Python decorators tend use this.

Read
Read
08
Making a decorator that accepts arguments
4 min read Screencast available Premium

If you want to make a decorator that's customizable, make a decorator that accepts arguments. That's really just a function that accepts arguments and returns a decorator that uses those arguments.

Read
Read
09
Making a class decorator
2 min read Screencast available Premium

Decorators aren't just for functions! Class decorators are functions that accepts a class and return a class. They usually modify and then return the original class instead of making a new class.

Read
Read
10
Decorators aren't always functions
6 min read Screencast available Premium

Decorators are not necessarily implemented using functions. You could also make a decorator using a class, or even an instance of a class. Any callable could be a decorator.

Read
Read
11
Decorators can return anything
4 min read Screencast available Premium

Decorators can return anything, including non-functions and non-classes, which is rare but occasionally useful.

Read
Read
12
Storing attributes on functions
3 min read Screencast available Premium

Functions are objects in Python, and they can store attributes. Function attributes are usually used to store metadata related to a function.

Read
Read
13
Enclosing scope
4 min read Screencast available Premium

Python resolves variable names through its four scope levels: local, enclosing, global, and built-in.

Read
Read
14
Decorators with optional arguments
5 min read Screencast available Premium

Some decorators with or without arguments. How is that possible?!

Read
Read
Continue exploring
Profile picture of Trey
Learn something new about Python every week

My name is Trey Hunner. I publish new Python articles and screencasts every week through Python Morsels. If you want to learn something new about Python every week, join Python Morsels!

Join Python Morsels ✨