zoukankan      html  css  js  c++  java
  • python学习之路 六 :装饰器

    本节重点:

    • 掌握装饰器相关知识

        python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使用python装饰器的好处就是在不用更改原函数的代码前提下给函数增加新的功能。 

    装饰器扩展登录功能

    import json
    
    
    def auth_user(username, password):
        user_dict = json.load(open("file/user1.txt", "r", encoding="gbk"))
        if username in user_dict:
            if password == user_dict[username]:
                return True
        return False
    
    
    login_status = False
    
    
    def login(fun):
        def inner(*args, **kwargs):
            global login_status
            if not login_status:
                username = input("用户名:").strip()
                password = input("密码:").strip()
                if auth_user(username, password):
                    login_status = True
                else:
                    print("wrong username or password")
            if login_status:
                fun(*args, **kwargs)
    
        return inner
    
    
    @login
    def dalu():
        print(" 欢迎来到大陆电影 ".center(30, "-"))
    
    
    @login
    def hongkong():
        print(" 欢迎来到香港电影 ".center(30, "-"))
    
    
    @login
    def rihan():
        print(" 欢迎来到日韩电影 ".center(30, "-"))
    
    
    @login
    def oumei():
        print(" 欢迎来到欧美电影 ".center(30, "-"))
    
    
    action_dict = {
        1: dalu,
        2: hongkong,
        3: rihan,
        4: oumei
    }
    
    if __name__ == '__main__':
        while True:
            choice = input("""--- 选择功能 ---
        1.大陆电影
        2.香港电影
        3.日韩电影
        4.欧美电影
    choice:""").strip()
            if choice.isdigit() and int(choice) in action_dict:
                action_dict[int(choice)]()

    可选择登录方式的装饰器(带参数的装饰器)

    import json
    
    
    def auth_user(auth_type, username, password):
        auth_data = json.load(open("file/user2.txt", "r", encoding="gbk"))
        user_list = auth_data[auth_type]
        if username in user_list:
            if password == user_list[username]:
                return True
        return False
    
    
    login_status = False
    
    
    def login(auth_type):
        def auth(fun):
            def inner(*args, **kwargs):
                global login_status
                nonlocal auth_type
                if not login_status:
                    username = input("用户名:").strip()
                    password = input("密码:").strip()
                    if auth_user(auth_type, username=username, password=password):
                        login_status = True
                    else:
                        print("wrong username or password")
                if login_status:
                    fun(*args, **kwargs)
            return inner
        return auth
    
    
    # @login("qq")
    def dalu():
        print(" 欢迎来到大陆电影 ".center(30, "-"))
    
    
    # @login("wechat")
    def hongkong():
        print(" 欢迎来到香港电影 ".center(30, "-"))
    
    
    # @login("qq")
    def rihan():
        print(" 欢迎来到日韩电影 ".center(30, "-"))
    
    
    # @login("qq")
    def oumei():
        print(" 欢迎来到欧美电影 ".center(30, "-"))
    
    
    action_dict = {
        1: dalu,
        2: hongkong,
        3: rihan,
        4: oumei
    }
    
    if __name__ == '__main__':
        while True:
            choice = input("""--- 选择功能 ---
        1.大陆电影
        2.香港电影
        3.日韩电影
        4.欧美电影
    choice:""").strip()
            if choice.isdigit() and int(choice) in action_dict:
                auth_type = None
                if not login_status:
                    auth_type = input("登录方式").strip()
                auth = login(auth_type)
                inner = auth(action_dict[int(choice)])
                inner()
  • 相关阅读:
    VOA 2009/11/02 DEVELOPMENT REPORT In Kenya, a Better Life Through Mobile Money
    2009.11.26教育报道在美留学生数量创历史新高
    Java中如何实现Tree的数据结构算法
    The Python Tutorial
    VOA HEALTH REPORT Debate Over New Guidelines for Breast Cancer Screening
    VOA ECONOMICS REPORT Nearly Half of US Jobs Now Held by Women
    VOA ECONOMICS REPORT Junior Achievement Marks 90 Years of Business Education
    VOA 2009/11/07 IN THE NEWS A Second Term for Karzai; US Jobless Rate at 10.2%
    Ant入门
    Python 与系统管理
  • 原文地址:https://www.cnblogs.com/LTEF/p/9430826.html
Copyright © 2011-2022 走看看