day 12 Summary

1. closure function

Closure function: closed (closed / off) package (closure function inside function) - "internal function returns a function closure

import requests

def f1():
    def f2():
        print('from f2')
        
    return f2
f2 = f1()
f2()
----------------------------------------------------------
def f1(url):
    def spider():
        print(url)
        
    return spider  # 闭包函数

Closure function: the interior of the closure function closure function + variable internal closure function with a function of both the package and then returned through the return value in the form of out

Closures have to function at least in line with nested functions

def f1(url):
    def f2():
        print(url)
        pass
    
    return f2
res = f1(1)
res()

2. decorator

Decorator: Decorative (new adds an extra feature)

Nature decorator is a function of increased functionality to the function

Decorator: function to increase the functional capabilities, with two caveats increased functionality:

  1. Without changing the source code of the original function

  2. It does not change the original function is called

import time

def index():
    """被装饰的函数"""
    print('index')
    time.sleep(1)
    
index()
# 增加功能:计时功能

---------------------------------------------------------
def index():
    """被装饰的函数"""
    start = time.time()
    print('index')
    time.sleep(1)
    end = time.time()
    print(end-start)
    
index()
-----------------------------------------------------------
def index():
    """被装饰的函数"""
    print('index')
    time.sleep(1)
    
    
start = time.time()  # 获取当前时间
index()
end = time.time()
print(end-start)

def index1():
    print('index1')
   
# 改变调用方式
---------------------------------------------------------------
def index():
    """被装饰函数"""
    print('index')
    time.sleep(1)
    
def time_count(func):
    start =time.time()
    func()
    end = time.time()
    print(end-start)
    

A decorative itself is a function without the function to be decorated decoration

2. decorator decorative function does not change the source code to be decorated function

3. decorator decorative function call does not change the way the decorative function

# v1
import time


def index():
    """被装饰的函数"""
    print('index')
    time.sleep(1)
    
# time_count装饰器:对被装饰函数计时时
def time_count(func)  # func才是真正的index
    """装饰器"""
    
    def wrapper():
        start = time.time()
        func()
        end = time.time()
        print(end - start)
        
    return wrapper

index = time_count(index) # index == wrapper
index()  # wrapper()
----------------------------------------------------------------------
# v2:带返回值
import time

def index():
    """被装饰的函数"""
    print('x',x)
    print('index')
    time.sleep(1)
    
    return 'index'


# time_count装饰器:对被装饰函数计时
def tiem_count(func):  # func才是真正的index
    """装饰器"""
    
    def wrapper():
        start = time.time()
        res = func()  # index()
        res = func()  # index()
        end = time.time()
        print(end - start)
        
        return res
    
    return wrapper

index = time_count(index)  # index == wrapper
res = index()  # wrapper()
print(res)
----------------------------------------------------------------------------

# v3:加参数
import time

def index(x,y,z = 10):
    """被装饰函数"""
    print('x',x)
    print('index')
    time.sleep(1)
    
    return 'index'


# time_count装饰器:对被装饰函数计时
def time_count(func)  # func才是真正的index
    """装饰器"""
    
    def wrapper(*args,**kwargs):  # (10,20)  # *args和kwargs接收所有参数
        start = time.time()
        res = func(*args,**kwargs)  # index()  # *(10,20)  # *args和swargs打散参数传给真正的index
        end  = time.time()
        print(end - start)
        
        return res
    return wrapper

index = time_count(index)  # index == wrapper
res = index(10,20,320)  # wrapper()
print(rse)
        
        

Login decorator:

username_list = []
def login_deco(func):
    def wrapper(*args,**kwargs):
        
        if username_list:
            print('已经登录,请勿重复登录')
            res = func(*args,**kwargs)
            return res
        
        username_inp = input('请输入用户名:')
        pwd_inp = input('请输入密码:')
        
        with open('user_info.txt', 'r', encoding='utf8') as fr:
            for user_info in fr:
                username, pwd = user_info.strip().split(':')
                if username_inp == username and pwd_inp == pwd:
                    print('登录成功')
                    username_list.append(username)
                    
                    res= func(*args,**kwargs)
                    return res
                
            else:
                print('登录失败')
                
    return wrapper

@login_deco  # index = login_deco(index)
def index(x,y):
    print('index')
    print('x,y',x,y)
    
    return 213

res = index(10,20)

Layer decorator

  1. For decorative functions, it is a function of the nature of
  2. Function does not change the source code
  3. Do not change the function call

Decorator template, if you really do not understand the decorator remember template

def deco(func):
    def wrapper(*arge,**kwargs):
        # 要加什么功能就加上去
        res = func(*args,**kwargs)
        
        return res
    
    return wrapper

3. The three-decorator

username_list = []

def sanceng(role):
    def login_deco(func):
        def wrapper(*arge,**kwargs):
            
            if username_list:
                print('已经登录,请勿重复登录')
                res = func(*args,**kwargs)
                return res
            
            username_inp= input('请输入用户名:')
            pwd_inp = input('请输入密码:')
            
            with open(f'{role}_info.txt','r',encoding='utf8') as fr:
                for user_info in fr:
                    username,pwd = username and pwd_inp == pwd
                    if sername_inp == username and pwd_inp == pwd:
                        print('登录成功')
                        username_list.append(username)
                        
                        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,20)

4. introduced iterator

  1. Integer
  2. Float
  3. String
  4. List
  5. Tuple
  6. set
  7. dictionary

Iterables: a method comprising __iter__ called iterables

Iterator: Contains __iter and ___next__ method called iterator

print(x)

print(s)

with open('user_infp.txt', 'r', encoding='utf8') as fr:
    fr.__next__()

Builder: yield keyword contains a function called generator

Guess you like

Origin www.cnblogs.com/LZF-190903/p/11574863.html