15.2 C
New York
Tuesday, September 26, 2023

Python Decorators Proficiency: Advanced Use Guide


Python designers are an effective function that permits you to customize the habits of functions or classes dynamically. Designers supply a method to include performance to existing code without customizing the initial source. This article will look into the principle of designers in Python, beginning with the essentials and slowly advancing to advanced methods.

Comprehending Designers

Function Designers

Function designers are a method to customize the habits of a function by covering it inside another function. The designer function takes the initial function as an argument, includes some performance, and returns a modified function. This permits you to boost or extend the habits of functions without customizing their source code.

 def uppercase_decorator( func):.
def wrapper():.
outcome = func().
return result.upper().
return wrapper.

def say_hello():.
return "Hi, World!".

print( say_hello()) # Output: HELLO, WORLD!

In the example above, the uppercase_decorator function is specified to cover the say_hello function. It customizes the habits by transforming the returned string to uppercase. The @uppercase_decorator syntax is utilized to use the designer to the say_hello function.

Class Decorators

Class designers resemble work designers however run on classes rather of functions. They enable you to customize the habits or include performance to a class. The designer function takes the initial class as an argument, develops an obtained class with included performance, and returns the customized class.

 def add_method_decorator( cls):.
def new_method( self):.
return "New approach included!".
cls.new _ approach = new_method.
return cls.

class MyClass:.
def existing_method( self):.
return "Existing approach called!".

obj = MyClass().
print( obj.existing _ approach()) # Output: Existing approach called!
print( obj.new _ approach()) # Output: New approach included!

In the example above, the add_method_decorator function covers the MyClass class and includes a brand-new approach called new_method The @add_method_decorator syntax is utilized to use the designer to the MyClass class.

Designer Syntax and Execution

When utilizing designers, it is very important to comprehend the order of execution. Designers are used from the bottom up, implying the designer specified at the top is performed last. This order is essential when several designers are used to the exact same function or class.

 def decorator1( func):.
print(" Designer 1 performed").
return func.

def decorator2( func):.
print(" Designer 2 performed").
return func.

def my_function():.
print(" Inside my_function").



 Designer 2 performed.
Designer 1 performed.
Inside my_function.

In the example above, the decorator2 designer is performed initially, followed by the decorator1 designer. The my_function is then called, and the output shows the order of execution.

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles