9.23 closures and decorators

1. closure function: the function of internal closure wrap variables and functions, the function returns the inside of the closure function. Closure function is a function.

def f1():
    def f2():
        print('from f2')
    return f2
res=f1()
res()   #from f2
def f1(url):
    def f2():
        print(url)
    return f2
res=f1('www.taobao.com')
res()  #www.taobao.com

Two kinds as a function parameter passing mode:

a. in the form of parameters

def f1(x):
    print(x)
f1(1) #1

b. contracted function

def outter(x):
    x=1
    def inner():
        print(x)
    return inner
f=outter(1)
f() #1

2. decorator: to add additional features are decorative objects, define a decorator is the definition of a function, but the function of the function is used to add extra functionality to other functions.

Achieve a decorator's two major principles must be observed:

a. without modifying the source code of the object to be decorated

b. does not modify the way calls are decorative objects

3. Use of the decorator

Changes in the source code

import time

def index():
    '''被装饰的函数'''
    start=time.time()
    print('welcome to index')
    time.sleep(1)
    end=time.time()
    print(f'运行时间是:{end-start}')
index()
运行结果:
welcome to index
运行时间是:1.0000569820404053

Write duplicate code:

import time
def index():
    print('index')
    time.sleep(1)

def f1():
    print('f1')
    time.sleep(1)

start=time.time()
index()
end=time.time()
print(end-start)

start=time.time()
f1()
end=time.time()
print(end-start)
运行结果:
index
1.0000574588775635
f1
1.0000569820404053

The first parameter passing mode: change the way calls

import time
def index():
    print('index')
    time.sleep(1)

def time_count(func):
    start = time.time()
    func()
    end=time.time()
    print(end-start)
time_count(index) 
运行结果:
index
1.0000572204589844

The second parameter passing mode: packet function - Outsourcing

import time
def index():
    print('index')
    time.sleep(1)

def time_count(func):   #func是真正的index
    def wrapper():
        start = time.time()
        func()
        end=time.time()
        print(end-start)
    return wrapper

res=time_count(index)  #res==wrapper
res()
运行结果:
index
1.0010573863983154

4. Improve decorator:

wrapper return value and return the same value of the index

import time
def index():
    print('index')
    time.sleep(1)
    return 123

def time_count(func):   #func是真正的index
    def wrapper():
        start = time.time()
        func()
        end=time.time()
        print(end-start)
        return res    
    return wrapper

f1=time_count(index)  #res==wrapper
res=f1()
print(res)
运行结果:
index
1.0010573863983154
123

To index parameter passing achieve:

import time
def index():
    print('index')
    time.sleep(1)

def home(name):
    print(f'welcome {name} to home page')
    time.sleep(1)
    return name

def time_count(func):   #func是真正的index
    def wrapper(*args,**kwargs):
        start = time.time()
        res=func(*args,**kwargs)
        end=time.time()
        print(end-start)
        return res
    return wrapper

res=time_count(home) #res==wrapper
f1=res('nick')
print(f1)
运行结果:
welcome nick to home page
1.0000569820404053
nick

The decorator syntactic sugar: In the decorative function immediately above, and is a single line decorator!

def time_count(func):   #func是真正的index
    def wrapper(*args,**kwargs):
        start = time.time()
        res=func(*args,**kwargs)
        end=time.time()
        print(end-start)
        return res
    return wrapper

@time_count
def index():
    print('index')
    time.sleep(1)
    return 123

@time_count
def home(name):
    print(f'welcome {name} to home page')
    time.sleep(1)
    return name

# res=time_count(home) #res==wrapper
f1=home('nick')
print(f1)

f2=index()
print(f2)
运行结果:
welcome nick to home page
1.0000572204589844
nick
index
1.0000572204589844
123

6. decorator template:

def deco(func):
    def wrapper(*args,**kwargs):
        res=func(*args,**kwargs)
        return res
    return wrapper

7. There are three parameters decorator:

usename_list=[]

def sanceng(role):
    def login_deco(func):
        def wrapper(*args,**kwargs):
            usename_inp=input('请输入用户名:')
            pwd_inp=input('请输入密码:')

            if usename_list:
                print('已经登录,无须重复')
                res=func(*args,**kwargs)
                return res
            with open(f'{role}_info.txt','r',encoding='utf8') as fr:
                for role_info in fr:
                    usename,pwd=role_info.strip().split(':')
                    if usename==usename_inp and pwd==pwd_inp:
                        print('登录成功')
                        usename_list.append(usename)
                        res = func(*args, **kwargs)
                        return res
                else:
                    print('登录失败')
        return wrapper
    return login_deco

@sanceng('admin')
def index (x,y):
    print('index')
    print('x,y',x,y)

    return 123

res=index(10,30)
运行结果:
请输入用户名:ldd
请输入密码:123
登录成功
index
x,y 10 30

Guess you like

Origin www.cnblogs.com/lidandanaa/p/11574686.html