Decorators in Python

Photo by Thalia Ruiz on Unsplash

Decorators in Python

One of the important programming patterns is the decorator pattern.

What do decorators do? They modify a function's behavior, allowing us to enhance to add functionality without changing its structure. But how does it work in Python?

You might have noticed the syntactic sugar notation for the decorator - the so-called 'pie' notation: @.

def my_function():
    do something

Let's look at a quick practical example. We're going to define a 'polite' decorator that will modify announcements issued in a train station.

We will then decorate a function issuing such an announcement.

Here's how it would look like:

Image preview

Thanks for reading!

Found it useful? Subscribe to my Analytics newsletter at