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 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 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 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 () 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?") 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() Result: Hello! wait 5s How are you? wait 5s GoodBye!

Mar 23, 2025 - 10:49
 0
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

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

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

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 ()

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?")

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()

Result:

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