Python Advanced - Decorator

Python Advanced - Decorator Introduction:

Python Advanced - Decorator.It is a function that adds extra functionality to an existing function, which is essentially a closure function.

1. Python Advanced - Decorator.Definition of decorator
It is a function that adds extra functionality to an existing function, which is essentially a closure function .
Decorator features:
1.Do not modify the source code of existing functions
2.Do not modify the calling method of existing functions
3.Add extra functionality to an existing function

2. Python Advanced - Decorator.Sample code for the decorator


# Add a login verification function
def check( fn ):
def inner( ):
print("Please login first...")
fn ( )
return inner
def comment( ):
print("Post a comment")
# Use decorators to decorate functions
comment = check(comment)
comment( )
#Basic prototype of the decorator
# def decorator( fn ): # fn : target function.
# def inner( ):
# '''Before executing the function'''
# fn () # Execute the decorated function
# '''After executing the function'''
# return inner
Code Description:
•The closure function has one and only one parameter, which must be a function type, so that the function defined in this way is a decorator.
•Writing code should follow the open and closed principle, which stipulates that the functional code that has been implemented is not allowed to be modified, but can be extended.
Results of the:
please log in first....
Comment

3. Python Advanced - Decorator.Syntax sugar for decorators


If there are multiple functions that need to add the function of login verification, each time you need to write code like func = check( func ) to decorate the existing functions, which is still rather troublesome.
Python provides a simpler way to write decorative functions, which is syntactic sugar. The writing format of syntactic sugar is: @decorator name , and the decoration of existing functions can also be completed by means of syntactic sugar.
# Add a login verification function
def check( fn ):
print("The decorator function is executed")
def inner( ):
print("Please login first...")
fn ( )
return inner
# Use syntactic sugar to decorate functions
@check
def comment( ):
print("Post a comment")
comment( )
illustrate:
•@check is equivalent to comment = check(comment)
•The execution time of the decorator is as soon as the module is loaded.
Results of the:
please log in first....
Comment

4.Python Advanced - Decorator.Summary
•decorator is essentially a closure function that extends an existing function with additional functionality.
•The syntax of the decorator is :
# decorator
# def decorator( fn ): # fn : The decorated target function.
# def inner( ):
# '''Before executing the function'''
# fn () # Execute the decorated target function
# '''After executing the function'''
# return inner
The syntax sugar usage of decorators: @ decorator name , which can also complete the decoration operation on existing functions.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us