DEV Community

Cover image for Explain Python decorator for kids🧒

Explain Python decorator for kids🧒

Mahmoud EL-kariouny on March 09, 2025

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 actu...
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 🙋