Skip to main content

Command Palette

Search for a command to run...

Decorators in Python

Updated
1 min read
Decorators in Python
C

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified

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: @.

@decorator
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:

Thanks for reading!

Found it useful? Subscribe to my Analytics newsletter at notjustsql.com.

More from this blog

D

Datawise — SQL, BigQuery & Python for Data Engineers

205 posts

Data Engineer with a passion for transforming complex data landscapes into insightful stories. Here on my blog, I share insights, challenges, and the ever-evolving dance of technology and business.