• 函数装饰器


    装饰器又称语法糖,以下代码是装饰器没有简写之前:

    # __*__ coding: utf-8 __*__
    __author__ = "David.z"
    user_status = False
    def login(func):
        def inner():#嵌套函数
            _username = "alex"  # 假装这个DB里存的用户信息
            _password = "abc123"  # 假装这是DB里存的用户密码
            global user_status
    
            if user_status == False:
                username = input("user:").strip()
                password = input("password:").strip()
    
                if username == _username and password == _password:
                    print("welcome login...")
                    user_status = True
                else:
                    print("wrong username or password!")
            else:
                print("用户已登录,验证通过。。。")
            if user_status:
                func()
        return inner
    def home():
        print("首页".center(15,"*"))
    def america():
        print("欧美专区".center(15, "*"))
    def japan():
        print("日韩专区".center(15,"*"))
    def henan():
        print("河南专区".center(15,"*"))
    henan = login(henan) #inner
    
    print(henan) #打印出来看看是不是内存地址
    henan() #加上()执行函数

    以下代码是简写之后:

    唯一区别就是在装饰的时候用@login

    # __*__ coding: utf-8 __*__
    __author__ = "David.z"
    user_status = False
    def login(func):
        def inner():#嵌套函数
            _username = "alex"  # 假装这个DB里存的用户信息
            _password = "abc123"  # 假装这是DB里存的用户密码
            global user_status
    
            if user_status == False:
                username = input("user:").strip()
                password = input("password:").strip()
    
                if username == _username and password == _password:
                    print("welcome login...")
                    user_status = True
                else:
                    print("wrong username or password!")
            else:
                print("用户已登录,验证通过。。。")
            if user_status:
                func()
        return inner
    def home():
        print("首页".center(15,"*"))
    def america():
        print("欧美专区".center(15, "*"))
    def japan():
        print("日韩专区".center(15,"*"))
    @login
    def henan():
        print("河南专区".center(15,"*"))
    # henan = login(henan) #inner
    #
    # print(henan) #打印出来看看是不是内存地址
    henan() #加上()执行函数

    最后一点改变,现在是在调用函数里面加了参数调用,为了不影响其他函数的调用。需要在嵌套的函数里加入*args,**kwargs

    # __*__ coding: utf-8 __*__
    __author__ = "David.z"
    user_status = False
    def login(func):
        def inner(*args,**kwargs):#嵌套函数
            _username = "alex"  # 假装这个DB里存的用户信息
            _password = "abc123"  # 假装这是DB里存的用户密码
            global user_status
    
            if user_status == False:
                username = input("user:").strip()
                password = input("password:").strip()
    
                if username == _username and password == _password:
                    print("welcome login...")
                    user_status = True
                else:
                    print("wrong username or password!")
            else:
                print("用户已登录,验证通过。。。")
            if user_status:
                func(*args,**kwargs)
        return inner
    def home():
        print("首页".center(15,"*"))
    def america():
        print("欧美专区".center(15, "*"))
    @login
    def japan(): print("日韩专区".center(15,"*")) @login def henan(style): print("河南专区,--%s".center(15,"*")%style) #henan = login(henan) #inner #japan = login(japan) print(henan) #打印出来看看是不是内存地址 henan("3P") #加上()执行函数 japan()

    两层嵌套函数,并带入参数的装饰器:

    # __*__ coding: utf-8 __*__
    __author__ = "David.z"
    user_status = False
    def login(func2):
        def outer(func):
            def inner(*args, **kwargs):
                _username = "alex"  # 假装这个DB里存的用户信息
                _password = "abc123"  # 假装这是DB里存的用户密码
                global user_status
                if user_status == False:
                    username = input("user:").strip()
                    password = input("password:").strip()
    
                    if username == _username and password == _password:
                        print("welcome login...")
                        user_status = True
                    else:
                        print("wrong username or password!")
                else:
                    print("用户已登录,验证通过。。。")
                if user_status:
                    func(*args, **kwargs)
    
            return inner
        return outer
    def home():
        print("首页".center(15,"*"))
    
    def america():
        print("欧美专区".center(15, "*"))
    @login("weixin")
    def japan():
        print("日韩专区".center(15,"*"))
    @login("qq")
    def henan(style):
        print("河南专区,---%s".center(15,"*")%style)
    
    henan("3P")
    japan()
  • 相关阅读:
    ConCurrent并发包
    volitale最经典理解
    java中关于volatile的理解疑问?
    一直对zookeeper的应用和原理比较迷糊,今天看一篇文章,讲得很通透,分享如下(转)
    架构师成长之路
    rocketmq,zookeeper,redis分别持久化的方式
    rocketmq持久化方式
    源码主干分支开发四大模式
    HDU 4876 ZCC loves cards(暴力剪枝)
    webView用法小结
  • 原文地址:https://www.cnblogs.com/davidz/p/9143961.html
Copyright © 2020-2023  润新知