DEV Community

Cover image for Explain Python decorator for kids🧒
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on • Edited on

36 1 1 1 2

Explain Python decorator for kids🧒

Hey there! Today, I’m going to explain something really cool in Python called decorators. It might sound like a big word, but don’t worry it’s actually pretty simple, and I’ll use fun examples to show you how it works.

What’s a Decorator?

In Python, a decorator is like a magic box you can put a function into. A function is just a little instruction that tells the computer to do something, like say hello or calculate a number. When you put the function in the magic box (the decorator), it comes out with extra powers—without you having to change the function itself!

Think of it like this: You have a toy car that moves. It’s great, but what if you want it to make a cool sound or go faster? You don’t want to rebuild the car; you just want to add something extra. A decorator is like adding a booster or a sound button to your toy—it makes it more fun without changing what it already does.

Let’s look at some examples to make it super clear.

Example 1: A Toy That Says "Hi!"

Let’s say you have a toy that talks. You want it to say "Hi!" before it does its job. Here’s how a decorator works:

# This is our "sticker" (decorator)
def say_hi(func):
    def new_toy():
        print("Hi!")
        func()  # The original toy still works
    return new_toy

# This is our toy (function)
@say_hi  # Slap the sticker on!
def talk():
    print("I’m a robot toy!")

# Play with the toy
talk()
Enter fullscreen mode Exit fullscreen mode

If we use it like this: talk(), it prints:

Hi!
I’m a robot toy!
Enter fullscreen mode Exit fullscreen mode

Example 2: A Singing Toy

Now let’s make a toy that sings a little song before it works!

# Our sticker (decorator)
def sing_song(func):
    def new_toy():
        print("🎵 La la la! 🎵")
        func()  # The toy does its thing
    return new_toy

# Our toy
@sing_song  # Add the singing sticker!
def dance():
    print("I’m dancing!")

# Play with it
dance()
Enter fullscreen mode Exit fullscreen mode

If we use it like this: dance(), it prints:

🎵 La la la! 🎵
I’m dancing!
Enter fullscreen mode Exit fullscreen mode

Note:
The naming convention for the inner function wrapper should follow best practices.

Why Are Decorators Awesome?

So, decorators are like special wrappers you can put around your functions to give them extra powers. They’re great because:

  • You don’t have to change the original function.
  • You can use the same decorator on lots of functions.
  • They make your code more fun and flexible.

Think of it like decorating a gift. The gift inside stays the same, but you add pretty paper or a bow to make it special. In Python, the function is the gift, and the decorator is the wrapping that makes it even better.

Connect with Me 😊

🔗 Links

linkedin

twitter

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (22)

Collapse
 
ramkumar-m-n profile image
Ramkumar M N

Hi Mahmoud EL-kariouny,
Good work.

Add py in your code tag for better readability and visibility, it will look like below.

# This is our "sticker" (decorator)
def say_hi(func):
    def new_toy():
        print("Hi!")
        func()  # The original toy still works
    return new_toy

# This is our toy (function)
@say_hi  # Slap the sticker on!
def talk():
    print("I’m a robot toy!")

# Play with the toy
talk()
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

Hi, Ramkumar

Thank you for your comment and suggestion.
Yes, it will be very helpful and cool 😎

But I don't know how to add py to my code😥🙄 because I know much in the mark down language

Collapse
 
ramkumar-m-n profile image
Ramkumar M N • Edited

include space py after initial code marker, like
‘’’ py

Thread Thread
 
mahmoudessam profile image
Mahmoud EL-kariouny

Thank a lot bro 🤗, I updated the post
I hope you see it well now I learned new thing from you thank again 😎

Thread Thread
 
ramkumar-m-n profile image
Ramkumar M N

Hi Mahmoud EL-kariouny,
No problem, we're here to help each other.

Yes, it looks great now.

Regards,
Ram

Thread Thread
 
mahmoudessam profile image
Mahmoud EL-kariouny

Yes, we here to help each other Ram
Nice to meet you

Be safe
Mahmoud

Collapse
 
mosesb profile image
Moses B

why not just call say_hi inside the talk() function, why use decorators? are there any special benefits?

Collapse
 
ang_hanwei_f6656d6421a7a profile image
Ang Han Wei

Calling say_hi() inside talk() works only once. If you require the feature in say_hi(), you need to repeat it in other functions. Decorators allow yoy to just define it once and use it repeatedly

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

Thank you for your explanation 🤗😊😎

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

I hope it's clear now for you

Collapse
 
inti_soto profile image
Inti Soto

Thanks for the simple and entertaining explanation, Mahmoud 👍🏻

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

It's my pleasure 😎
Thank you for your comment 🤗😊

Collapse
 
madhurima_rawat profile image
Madhurima Rawat

This is great analogy. Great article 👏

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

Thank you 🤗😊😎

Collapse
 
mohalac6 profile image
Mohalac6

That's awesome 👌

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

Thank you 🤗😊😎

Collapse
 
abouziad profile image
عادل المغربي

Great explanation

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

Thank you 🤗😊😎

Collapse
 
oddalerts profile image
OddAlerts

The quality of product surpassed my expectations!

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

Thank you 🤗😊😎

Collapse
 
parthasarathy_manogar_859 profile image
parthasarathy manogar

How to use a main function with parameters

Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

I'm sorry 🙏 but I don't understand your question 🙋

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay