python菜鸟学习: 12. 装饰器的中级用法

2022/8/25 1:23:00

本文主要是介绍python菜鸟学习: 12. 装饰器的中级用法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

# -*- coding: utf-8 -*-

username = "liyuzhoupan"
password = "123"


def author(wrappreType):
    print("wrappreType:", wrappreType)

    def outterwrapper(func):
        def wrapper(*args, **kwargs):
            if wrappreType == "login1":
                username1 = input("Username:").strip()
                password1 = input("Password:").strip()
                if username == username1 and password == password1:
                    print("\033[32;1m Login success!\033[0m")
                    return func(*args, **kwargs)
                else:
                    exit("\033[31;1m login fail \033[0m")
            elif wrappreType == "login2":
                print("login2 login")
                return func(*args, **kwargs)
            else:
                print("xia J8 che")

        return wrapper

    return outterwrapper


def hello_index():
    print("welcome to index")


@author(wrappreType="login1")  # 调用装饰器,根据条件wrappreType进行调用
def hello_home():
    print("this is hello_home")
    return "from home"


@author(wrappreType="login2")
def hello_bbs():
    print("this is hello_index")
    return "from bbs"


hello_index()
print(hello_home())
print(hello_bbs())


这篇关于python菜鸟学习: 12. 装饰器的中级用法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程