Python Decorators? meh...

Hamza MateenHamza Mateen
1 min read

In the following snippet, ...

The 'print_name' function becomes the input of the 'decorate' decorator. This is possible because Python considers its functions as First Class Citizens.
In other words, functions can be assigned to variables and passed as arguments.

def decorate (the_function_to_be_decorated):
    def the_function_after_the_decoration (arguments):
        print("some decoration before calling the function")
        the_function_to_be_decorated(arguments)
        print("some decoration after calling the function")

    return the_function_after_the_decoration

@decorate
def print_name (name):
    print("Hi, ", name)

print_name("Hamza Mateen")

Output

I hope you enjoyed it, and learned it in a func way!

0
Subscribe to my newsletter

Read articles from Hamza Mateen directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hamza Mateen
Hamza Mateen

Web developer with expertise in React, Flask, automation, and a passion for exploring diverse programming languages and computing paradigms.