Function decorator acquaintance

eg: 1 Charles Lee, working at a xx Technology Co., Ltd., in charge of arrangements for a task,
write code to test the efficiency of a brother to write a function hate hate.
import time (one module)
DEF index ():
the time.sleep (2)
Print ( 'Welcome to the blog Home Park')

Computational efficiency operation function
Print (the time.time ())
START_TIME the time.time = ()
index ()
END_TIME = the time.time ()
Print (F 'the efficiency of this function {end_time-start_time}')

  1. Director allows you to test Deng, Li elephant, too much duplicate code.
    func1 DEF ():
    the time.sleep (2)
    Print ( 'Welcome Home Diary')

func2 DEF ():
the time.sleep (1)
Print ( 'Welcome to Review Home')

= the time.time START_TIME ()
func1 ()
END_TIME = the time.time ()
Print (F 'the efficiency of this function {end_time-start_time}')

= the time.time START_TIME ()
func2 ()
END_TIME = the time.time ()
Print (F 'the efficiency of this function {end_time-start_time}')

  1. Integrated into the function
    DEF func1 ():
    the time.sleep (2)
    Print ( 'Welcome Home Diary')

func2 DEF ():
the time.sleep (1)
Print ( 'Welcome to Review Home')

test_time DEF (X):
START_TIME the time.time = ()
X ()
END_TIME the time.time = ()
( 'the efficiency of this function END_TIME-START_TIME} {' F) Print
test_time (func1)
test_time (func2)

  1. Hate hate 500 Columbia This function is executed in the actual project, the master asked: when performing this function, it is executed at the same time to test the efficiency of the function.
    index DEF ():
    the time.sleep (2)
    Print ( 'Welcome to the blog Home Park')
    index ()

test_time DEF (X):
START_TIME the time.time = ()
X ()
END_TIME = the time.time ()
Print (F 'the efficiency of this function {end_time-start_time}')

test_time(index)

4 version of the problem: the principle of openness satisfied, closed principle: do not change the source code of the original function, and invocation. Violation of the principle of closure: Change the function is called.

Version 5: You can not change the way call the original function (closure):
DEF index ():
the time.sleep (2)
Print ( 'Welcome to the blog Home Park')
index ()

func1 DEF ():
the time.sleep (2)
Print ( 'Welcome Home Diary')

test_time DEF (X): X = index
DEF Inner ():
START_TIME the time.time = ()
X ()
END_TIME the time.time = ()
( 'the efficiency of this function END_TIME-START_TIME} {' F) Print
return Inner
index test_time = (index)
index ()

@ Decorator syntactic sugar plus function name
DEF F ():
Print (666)
F = 'Bai'
Print (F)

test_time DEF (X): X = index
DEF Inner ():
START_TIME the time.time = ()
X ()
END_TIME the time.time = ()
( 'the efficiency of this function END_TIME-START_TIME} {' F) Print
return Inner

index = test_time @test_time (index)
DEF index ():
the time.sleep (2)
Print ( 'Welcome to the blog Home Park')
index = test_time (index)
index ()

func1 DEF ():
the time.sleep (2)
Print ( 'Welcome Home Diary')

@test_time
DEF func2 ():
the time.sleep (1)
Print ( 'Welcome to Review Home')

func2 = test_time(func2)
func3 = test_time(func3)
func2()

Version 6: decorated function returning
DEF test_time (X): X = index
DEF Inner ():
START_TIME the time.time = ()
RET = X ()
Print (F'ret: RET {} ')
END_TIME = Time. Time ()
( 'the efficiency of this function END_TIME-START_TIME} {' F) Print
return RET
return Inner

index = test_time @test_time (index)
DEF index ():
the time.sleep (0.5)
Print ( 'Welcome to the blog Home Park')
return True

print (index ()) inner ( )
you should make True returned to the index () so that it is perfect, but now the index is the inner, so if you do not completely change the use of the original function, you print (index ()) - -> True

Version 7: a decorative function parameters, whether plus without decorators, your argument of 'Taibaijinxing' should be passed to the parameter n ,. 6 can not be achieved but the version parameter passing, index ( 'Taibaijinxing') == inner ( 'Taibaijinxing')

test_time DEF (X): X = index
DEF Inner ( args, kwargs):
defined function: *
polymerization.
args = ( 'apple')
args = (. 1,. 3)
START_TIME the time.time = ()
RET = X (
args, kwargs)
execute a function of: *
break.
X = RET ( ( 'apple')) == x ( 'apple',)
RET = X (
(. 1,. 3)) == X (l, 3)
Print (F'ret: RET {} ')
END_TIME = the time.time ()
( 'the efficiency of this function END_TIME-START_TIME} {' F) Print
return RET
return Inner

index = test_time @test_time (index)
DEF index (n-):
the time.sleep (0.5)
Print (F '{n} Welcome to blog access Home Park')
return True

@test_time index = test_time(index)
def func2(a,b):
time.sleep(0.5)
print(f'最终结果:{a+b}')
return a + b

print (index ( 'apple')) inner ( 'apple')
Print (func2 (l, 3)) == Inner (l, 3)

warpper DEF (F):
DEF Inner (* args, ** kwargs):
'' 'is operated before the decoration function' ''
Print (666)
RET = F (* args, ** kwargs)
'' 'to be decorated function after the operation '' '
Print (' the finished ')
return RET
return Inner

@warpper
DEF FUNC ():
Print (111)
FUNC ()
FUNC ()
FUNC ()
FUNC ()
FUNC ()
application decorators: at source and invocation without changing the original function, its additional features .
Login authentication, print logs.

Guess you like

Origin www.cnblogs.com/-777/p/11076846.html