21 --- acquaintance python class finishing decorator

First, the decorator:

Essentially function, function: to add additional functionality to other functions

in principle:

1. The decorative function can make changes to the source code

2. You can not modify the function is called modified

A simple decorator

import time
def timmer(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        res = func(*args, **kwargs)
        stop_time = time.time()
        print('函数运行的时间%s'%(stop_time - start_time))

        return res
    return wrapper


@timmer
def cal(l):
    res = 0
    for i in l:
        time.sleep(0.01)
        res += i
    return res
ret = cal(range(200))
print(ret)

Second, the decorator's knowledge base

Higher-order function decorator = + + nested closure function

Higher-order function definition:

1. The function received parameter is a function name

2. The return value of the function is a function name

3. satisfies a condition of any of the above, higher order functions can be called

Analog decorator with higher-order functions:

The results run more than once

Time Import 
DEF foo (): 
    the time.sleep (. 3) 
    Print ( 'from foo') 

DEF Timmer (FUNC): 
    START_TIME the time.time = () 
    FUNC () 
    stop_time the time.time = () 
    Print ( 'run time function S is the% '% (stop_time - START_TIME)) 
    return FUNC 
foo = Timmer (foo) 
foo ()

  

Third, the nested functions and closures

Nesting is, re-defined functions in the function

Closure: closing -----> ----- variable packet encapsulated> a function layer

Each package variable start looking for that layer itself began to look for, find and then can not find from the outer layer

Key: function that is variable

DEF Father (name): 
    
    DEF Son (): 
        Print ( 'my father is% S'% name) 
        DEF Grandson (): 
            name = 'China' 
            Print ( 'my grandfather% S'% name) 
        Grandson () 
    Son () 
Father ( 'Henan')

Decorative frame

Timmer DEF (FUNC): 
    DEF wrapper (): 
        # add functionality 
        FUNC () 

    return wrapper
    

The decorator supplement shelf

Time Import 
DEF Timmer (FUNC): 
    DEF warpper (): 
        # increase functionality 
        START_TIME the time.time = () 
        FUNC () 
        stop_time the time.time = () 
        Print ( 'run time was S%'% (stop_time - START_TIME)) 
    return wrapper 

DEF Test (): 
    the time.sleep (2) 
    Print ( 'Test function is finished running') 

RES = Timmer (Test) # returns the address of the wrapper 
res () # wrapper is performed ()

As can be seen, as long as the function of the last two lines of code to a function of the received value: test

Namely :

test= timmer(test)

test() 

Decorator look in line with the principle of

But each time had to add functionality to a function to rewrite the test = timmer (test), is clearly unreasonable

Therefore, to replace with @timmer test = timmer (test)

Time Import 
DEF Timmer (FUNC): 
    DEF warpper (): 
        # increase functionality 
        START_TIME the time.time = () 
        FUNC () 
        stop_time the time.time = () 
        Print ( 'run time was S%'% (stop_time - START_TIME)) 
    return warpper 
@timmer # equivalent: = Timmer Test (Test) 
DEF Test (): 
    the time.sleep (2) 
    Print ( 'Test function is finished running') 
Test ()

Four, plus the return value of the decorator

Time Import 
DEF Timmer (FUNC): 
    DEF warpper (): 
        # increase functionality 
        START_TIME the time.time = () 
        RES = FUNC () # function test performed here, so to receive variables here a return value to the function 
        stop_time = time .time () 
        Print ( 'run time was S%'% (stop_time - START_TIME)) 
        return RES # here to test the return value returned 
    return warpper 
@timmer # equivalent: = Timmer test (test) 
DEF test (): 
    the time.sleep (2) 
    Print ( 'function test finishes running') 
    return 'this is a test of the return value of' 
a = test () 
Print (a)

Fifth, add parameters decorators

Time Import 
DEF Timmer (FUNC): 
    DEF wrapper (* args, ** kwargs): # accepts any value 
        # increasing function 
        START_TIME the time.time = () 
        RES = FUNC (* args, ** kwargs) # accept the wrapper value passed intact FUNC 
        stop_time the time.time = () 
        Print ( 'run time was S%'% (stop_time - START_TIME)) 
        return RES 
    return warpper 
@timmer # equivalent: = Timmer Test (Test) 
DEF test1 (name, Age, Gander): 
    the time.sleep (2) 
    Print ( 'test1 operation results to name% d% s age sex% s'% (name, Age, Gander)) 
    return 'test1 the return value is' 
a = test1 ( 'Jinling', 20 is, 'FEMALE') 
Print (A) 

@timmer     
DEF test2 (name, Age): 
    the time.sleep (. 3)
    print ( 'test2 result of the function is named% s age D%'% (name, Age))
    return 'test2 This is the return value of' 
B = test2 ( 'liuwen', 18 is) 
Print (B)

Sixth, to implement a validation decorator

func_ver DEF (FUNC): 
    DEF wrapper (* args, ** kwargs): 
        NAME1 the INPUT = ( 'Please enter your user name:') 
        pd = the INPUT ( 'Please enter your password:') 
        IF NAME1 == 'PJL' and pd == '123': 
            FUNC (* args, ** kwargs) 
        the else: 
            Print ( 'user name or password is incorrect') 

    return wrapper 




@func_ver 
DEF index (): 
    Print ( 'Welcome to my site') 
@func_ver 
Home DEF (name): 
    Print ( '% S Welcome Home'% name) 
@func_ver 
DEF Secrect (name): 
    Print ( '% S this is your little secret'% name) 


index () 
Home ( 'jinling') 
secrect ( 'jining')

 

 

  

Guess you like

Origin www.cnblogs.com/dabai123/p/11241830.html