Explain Python decorator for kids🧒
Mahmoud EL-kariouny

Mahmoud EL-kariouny @mahmoudessam

About: Software🌟developer👨‍💻

Location:
Egypt/Alex
Joined:
Jun 21, 2020

Explain Python decorator for kids🧒

Publish Date: Mar 9
40 22

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

Comments 22 total

  • Mohalac6
    Mohalac6Mar 11, 2025

    That's awesome 👌

  • Ramkumar M N
    Ramkumar M NMar 11, 2025

    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
    • Mahmoud EL-kariouny
      Mahmoud EL-kariounyMar 11, 2025

      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

      • Ramkumar M N
        Ramkumar M NMar 12, 2025

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

        • Mahmoud EL-kariouny
          Mahmoud EL-kariounyMar 12, 2025

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

          • Ramkumar M N
            Ramkumar M NMar 12, 2025

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

            Yes, it looks great now.

            Regards,
            Ram

            • Mahmoud EL-kariouny
              Mahmoud EL-kariounyMar 12, 2025

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

              Be safe
              Mahmoud

  • Madhurima Rawat
    Madhurima RawatMar 11, 2025

    This is great analogy. Great article 👏

  • عادل المغربي
    عادل المغربيMar 11, 2025

    Great explanation

  • parthasarathy manogar
    parthasarathy manogarMar 11, 2025

    How to use a main function with parameters

  • Moses B
    Moses BMar 12, 2025

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

    • Ang Han Wei
      Ang Han WeiMar 12, 2025

      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

    • Mahmoud EL-kariouny
      Mahmoud EL-kariounyMar 14, 2025

      I hope it's clear now for you

  •   OddAlerts
    OddAlerts Mar 13, 2025

    The quality of product surpassed my expectations!

  • Inti Soto
    Inti SotoMar 14, 2025

    Thanks for the simple and entertaining explanation, Mahmoud 👍🏻

Add comment