Blog 12

Advanced Functions

1. closure function

  • Closure function: function wrapped with the closure of both the closure variables inside a function of an internal function + closure function, and then returns the return value in the form.

    Thus, calls to functions can be carried out inside the closure function of the global

  • Closures have to meet the function of at least nested functions

def f1(url):  #f1就是闭包函数
    def spider():
        print(url)
    return spider  #函数对象
taobao = f1('www.taobao.com') #这里的 taobao 就是函数 spider
taobao() #打印结果: www.taobao.com

2. decorator

(1) Layer decorator

  • Decorator is an advanced application for closure function

  • Decorator of the nature and characteristics:

    • Nature decorator is a function

    • Decorator function is to increase the function is decorative function, increased functionality must meet at two points
      1. Without changing the source code of the function to be decorated
      2. Without changing the function is called to be decorated (by a previous call that is f1 (), then further decorated with f1 () call, is a function of the number of parameter decorated, the decoration, when calling f1 (), which brackets have to have how many arguments, no more and no less)
  • The following example is landing decorator

def zhuangshi(func):  # 接收到值后,func 就是函数: hanshu
    def login(*args, **kwargs):
        while True:
            user_inp = input('请输入用户名:')
            pwd_inp = input('请输入密码:')
            with open('user_info.txt', 'r', encoding='utf8')as fr:
                for user_info in fr:

                    user_name, pwd = user_info.strip().split(':')
                    if user_inp == user_name and pwd_inp == pwd:
                        print('登陆成功')
                        res = func(*args, **kwargs)
                        return res
                else:
                    print('登录失败')

    return login


@zhuangshi  # 双层装饰器,实现了:hanshu = zhuangshi(hanshu),必须放在被装饰函数的上一行
def hanshu(x, y):
    return x + y

print(hanshu(10, y=20))

(2) three-decorator

  • It is the outside layer of the double plus function

    Once again return below the second function layer

  • Three decorators to achieve the function parameter passing things to the 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: #选课系统中判断是哪一种登陆 :role为老师、学生和管理员
                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('user')
def index(x, y):
    print('index')
    print('x,y', x, y)
    return 123

res = index(10, 20)
print(res)

(3) Decorative template

# 二层装饰器模板
def deco(func):
    def wrapper(*args, **kwargs):
        # 要加什么功能就加上去
        res = func(*args, **kwargs)

        return res

    return wrapper

Guess you like

Origin www.cnblogs.com/Mcoming/p/11571908.html