day12 summary

Closure function

Closed (closed / off) bag (buns / stuffing the (closure variables inside a function) and a skin (a function of the internal closure function) wrap) - "internal closure function returns a function

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

Closures have to function at least in line with nested functions

Decorator

Decoration (a new addition of extra features) (Tools) - "Function

Nature decorator is a function of increased functionality to the function

Decorator: 1 itself is a decorative function, but it used to be decorated decoration function

  1. Decorative decorative function does not change the source code to be decorated function

  2. Decorator decorative function does not change the decorated function is called

Login decorator

# python装饰器语法糖(就是让代码更简洁)
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 123

Floor decorator:

  1. For decorative functions, it is essentially a function

  2. Function does not change the source code

  3. Do not change the function call

Three decorator

username_list = []


def sanceng(role):
    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(f'{role}_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

    return login_deco


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

    return 123


res = index(10, 20)

# 要记住装饰器的用途+装饰器的模板(多练);要了解装饰器至少要看3篇以上相关博客;要灵活运用至少5篇;要达到熟悉百度至少看10篇装饰器相关的博客

Iterator

Iterables: a method comprising __iter__ called iterables
iterator: __next__ method comprising __iter__ and called iterator

Guess you like

Origin www.cnblogs.com/zhm-cyt/p/11574050.html