DEV Community

An Vo
An Vo

Posted on

Python Decorator

DECORATOR SYNTAX

1- Define a decorator

def decorator_function(function):
    def wrapper_function():
        # Do something before the function
        function()
        # Do something after the function
    return wrapper_function
Enter fullscreen mode Exit fullscreen mode

Python Decorator Function (4 lines of code)

1- Create a normal function.
2- Input is another normal function, the function in this case doesn't have parentheses ().
3- Inside the decorator_function, create a wrapper_function
The wrapper_function() will trigger the actual input "function()" that was passed into the decorator_function().
4- Return the wrapper_function without parentheses ().

Key takeaways:
  + Decorator function is just a function that wraps another function,
  + And GIVE that function some ADDITIONAL FUNCTIONALITY
Enter fullscreen mode Exit fullscreen mode

2.1 - Using a decorator function by @ sign -> RECOMMENDED way.

@decorator_function #this line will trigger the decorator function for a_normal_function
def a_normal_function():
    pass
Enter fullscreen mode Exit fullscreen mode

2.2 - Using a decorator function by input function -> NOT RECOMMENDED way.

decorated_func = decorator_function(a_normal_function)
decorated_func() # remember to add parentheses ()
Enter fullscreen mode Exit fullscreen mode

EXAMPLES

Requirements:

  • All functions should be delayed by 5 seconds.
  • Avoid adding time.sleep(5) to each function.
  • Add time.sleep(5) once.

Implementation:

We can modify delay_decorator to delay the execution of the wrapped function by 5 seconds.

import time

def delay_decorator(function):
    def wrapper_function():
        time.sleep(5) # wait 5 seconds before executing the next function
        function()
    return wrapper_function


@delay_decorator
def say_hello():
    print("Hello!")

@delay_decorator
def say_bye():
    print("GoodBye!")

def say_greeting():
    print("How are you?")

Enter fullscreen mode Exit fullscreen mode

Now we will define a main_flow:

def main_flow():
    say_hello() # no delay because we don't add @delay_decorator to it
    say_greeting() # delay 5s before executing greeting
    say_bye() # delay 5s before executing GoodBye

main_flow()
Enter fullscreen mode Exit fullscreen mode

Result:

Hello!
wait 5s
How are you?
wait 5s
GoodBye!

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

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

If this article connected with you, consider tapping ❤️ or leaving a brief comment to share your thoughts!

Okay