Python - decorator function (a)

1. What is the function decorator?

    Decorator (Decorators) is an important part of Python.

    Simply put: they are modified functional capabilities of other functions, they help make our code shorter and more Fan!

    It can be said: In order to increase the function of the function or class is a grammatical function expansion. Like the girl makeup, like, girl or that girl, but after the makeup is more beautiful ~

 

2. Description code:

    First, we declare a basic function, and then call the function:

# Declare a basic function 
DEF test1 ():
     Print ( ' I am a basic function ' ) 
    
# call the function 
test1 ()

    After running outputs:  I am a basic function

    We need to add some of the features in this basic function output before and after:

# Declare a basic function 
DEF test ():
     Print ( ' I am a basic function ' ) 


# define a given test function to increase the functional capabilities 
DEF func2 (FUNC):
     # add functionality a 
    Print ( ' I was an increase in capability ' )
     # calling Elementary functions, here if you need to call the basic functions, we need the basic function passed as a parameter to func2 
    FUNC ()           # equivalent to calling the test function 
    # increase function two 
    Print ( ' I added features two ' ) 


# basic func2 passed as a parameter to the function, and call func2 
func2 (Test)

    Output will run: I added a feature

            I is a basic function

            I added two functions

    However, this is a function of increased functionality we run, we are not the most basic function test start, so we need to define a function, pass into tset function, and then return to the functions defined in the function in func2;

    Said may be a bit confused, we look at the code to understand the

# Declare a basic function 
DEF test ():
     Print ( ' I am a basic function ' ) 


# define a given test function to increase the functional capabilities 
DEF func2 (FUNC):
     DEF infunc ():
         # add functionality a 
        Print ( ' I increased functionality a ' )
         # calling Elementary functions, here if you need to call the basic functions, we need the basic function passed as a parameter to func2 
        FUNC ()           # equivalent to calling the test function 
        # increase function two 
        Print ( ' I am increasing function two ' )
     # here infunc can not add (), plus (after) becomes a function call infunc 
    #Returns a function, the function returns here is that we function better future to add features that have basic functions have also increased the function does 
    return infunc 


# Decorative operation 
test = func2 (test)
 # after the test call decorative function 
test ()

    The result: I added a feature

         I is a basic function

         I added two functions

    This is what we want to feature it! This is also the principle of the decorator , but we need to use decorator syntax to write;

    There may be some people did not understand the decoration operation: test = func2 (test) here func2 (test) is the value returned is a function of infunc, like the following code, you can run yourself down:

def test():
    print('hello,python')


func = test
func()

    With decorator syntax to write the above function:

# Define an increase to test the function function function 
DEF func2 (FUNC):
     DEF infunc ():
         # add functionality a 
        Print ( ' I'm increased functionality a ' )
         # Calling Elementary Functions, here if you need to call the basic functions, we need the basic function passed as a parameter to func2 
        FUNC ()   # equivalent to calling the test function 
        # increase function two 
        Print ( ' I added features two ' ) 

    # here infunc can not add (), plus it will become to call infunc (after) function 
    # returns a function, the function returns here is that we function better future to add features that have basic functions also have the function of increasing the function 
    return infunc 


# decorative operation 
@ func2   # equivalent = func2 the Test (the Test) 
# declared a basic function
DEF test ():
     Print ( ' I am a basic function ' ) 


# after the test call decorative function 
test ()   

    The result: I added a feature

         I is a basic function

         I added two functions

Guess you like

Origin www.cnblogs.com/cxstudypython/p/12142951.html