XVIII decorator

First, decorator

"" "
1, what is the decorator
is refers to the tool, can be defined as a function of
decorative it refers add something extra embellishment for other things

合到一起的解释:
    装饰器指的定义一个函数,该函数是用来为其他函数添加额外的功能

2. Why do we use a decorator
open closed principle
open: that is to extend the functionality is open to
closed: that is to modify the source code is closed

装饰器就是在不修改被装饰器对象源代码以及调用方式的前提下为被装饰对象添加新功能

3, How to
"" "
demand: add functionality to statistics runtime without modifying the source code and function index is called the

def index(x,y):
    time.sleep(3)
    print('index %s %s' %(x,y))

index(111,222)
# index(y=111,x=222)
# index(111,y=222)

Solution one: Failure
Problem: No modifications are called by decorative objects, but to modify its source code

import time

def index(x,y):
    start=time.time()
    time.sleep(3)
    print('index %s %s' %(x,y))
    stop = time.time()
    print(stop - start)

index(111,222)

Solution two: Failure
Problem: No modifications are called by decorative objects, nor modify its source code, and added new features
but the code redundancy

import time

def index(x,y):
    time.sleep(3)
    print('index %s %s' %(x,y))

start=time.time()
index(111,222)
stop=time.time()
print(stop - start)



start=time.time()
index(111,222)
stop=time.time()
print(stop - start)


start=time.time()
index(111,222)
stop=time.time()
print(stop - start)

Solution three: failure
problem: solved Option II code redundancy problem, but it will bring a new problem that the function is called change

import time

def index(x,y):
    time.sleep(3)
    print('index %s %s' %(x,y))

def wrapper():
    start=time.time()
    index(111,222)
    stop=time.time()
    print(stop - start)

wrapper()

Optimizing a program three: the index of the parameter to write live

import time

def index(x,y,z):
    time.sleep(3)
    print('index %s %s %s' %(x,y,z))

def wrapper(*args,**kwargs):
    start=time.time()
    index(*args,**kwargs) # index(3333,z=5555,y=44444)
    stop=time.time()
    print(stop - start)

# wrapper(3333,4444,5555)
# wrapper(3333,z=5555,y=44444)


方案三的优化二:在优化一的基础上把被装饰对象写活了,原来只能装饰index
import time

def index(x,y,z):
    time.sleep(3)
    print('index %s %s %s' %(x,y,z))

def home(name):
    time.sleep(2)
    print('welcome %s to home page' %name)


def outter(func):
    # func = index的内存地址
    def wrapper(*args,**kwargs):
        start=time.time()
        func(*args,**kwargs) # index的内存地址()
        stop=time.time()
        print(stop - start)
    return wrapper

index=outter(index) # index=wrapper的内存地址
home=outter(home) # home=wrapper的内存地址


home('egon')
# home(name='egon')

Three optimization program three: the wrapper is made with exactly the same decorative objects, real ones

import time

def index(x,y,z):
    time.sleep(3)
    print('index %s %s %s' %(x,y,z))

def home(name):
    time.sleep(2)
    print('welcome %s to home page' %name)

def outter(func):
    def wrapper(*args,**kwargs):
        start=time.time()
        res=func(*args,**kwargs)
        stop=time.time()
        print(stop - start)
        return res

return wrapper

Substitution: memory address pointing to the name of the home wrapper function
home = outter (home)

Home = RES ( 'Egon') # warpper RES = ( 'Egon')
Print ( 'Return Value - "', res)

Syntactic sugar: syntax make you happy

import time
装饰器
def timmer(func):
    def wrapper(*args,**kwargs):
        start=time.time()
        res=func(*args,**kwargs)
        stop=time.time()
        print(stop - start)
        return res  



return wrapper

@ Decorator write the name on a separate line is directly above the decorative objects

@timmer # index=timmer(index)
def index(x,y,z):
    time.sleep(3)
    print('index %s %s %s' %(x,y,z))

@timmer # home=timmer(ome)
def home(name):
    time.sleep(2)
    print('welcome %s to home page' %name)


index(x=1,y=2,z=3)
home('egon')

No reference decorator template summary

def outter(func):
    def wrapper(*args,**kwargs):
        # 1、调用原函数
        # 2、为其增加新功能
        res=func(*args,**kwargs)
        return res
    return wrapper

Guess you like

Origin www.cnblogs.com/marin0817/p/12554929.html