• python 装饰器


    一、函数装饰器

    1.从函数中返回函数

    在 if/else 语句中我们返回 greet 和 welcome,而不是 greet() 和 welcome()。为什么那样?这是因为当你把一对小括号放在后面,这个函数就会执行;然而如果你不放括号在它后面,那它可以被到处传递,并且可以赋值给别的变量而不去执行它。 

    def hi(name="yasoob"):
        def greet():
            return "now you are in the greet() function"
     
        def welcome():
            return "now you are in the welcome() function"
     
        if name == "yasoob":
            return greet
        else:
            return welcome
     
    a = hi()
    print(a)
    #outputs: <function greet at 0x7f2143c01500>
     
    #上面清晰地展示了`a`现在指向到hi()函数中的greet()函数
    #现在试试这个
     
    print(a())
    #outputs: now you are in the greet() function

    2.装饰器

    用函数decorator装饰function_requiring_decoration函数

    import time
    def decorator(func):
        def decorator_function():
            localtime = time.asctime(time.localtime(time.time()))
            print('before decorator running,time is', localtime)
            func()
            localtime = time.asctime(time.localtime(time.time()))
            print('after decorator running,time is ', localtime)
        #必须返回函数名,不带括号
        return decorator_function
    
    
    def function_requiring_decoration():
        a, b = 5, 3
        time.sleep(1)
        print("The result is ", a + b)
    
    function__decoration = decorator(function_requiring_decoration)
    
    if __name__=="__main__":
        function__decoration()

    以上代码的简写

    import time
    def decorator(func):
        def decorator_function():
            localtime = time.asctime(time.localtime(time.time()))
            print('before decorator running,time is', localtime)
            func()
            localtime = time.asctime(time.localtime(time.time()))
            print('after decorator running,time is ', localtime)
        #fanhuihans,bujiakuohao
        return decorator_function
    
    @decorator
    def function_requiring_decoration():
        a, b = 5, 3
        time.sleep(1)
        print("The result is ", a + b)
    
    if __name__=="__main__":
        function_requiring_decoration()

    结果

    before decorator running,time is Wed Jan 20 20:07:03 2021
    The result is  8
    after decorator running,time is  Wed Jan 20 20:07:04 2021

    二、类装饰器

    import time
    
    class Decorator(object):
        def __init__(self, func):
            self.func = func
        def call(self):
            localtime = time.asctime(time.localtime(time.time()))
            print ('before decorator running,time is',localtime)
            self.func() #run add function
            localtime = time.asctime(time.localtime(time.time()))
            print ('after decorator running,time is ',localtime)
    
    @Decorator
    def add():
        a,b=5,3
        time.sleep(1)
        print ("The result is ",a+b)
    if __name__=="__main__":
        add.call()

    结果,如上代码就完成了一个类装饰器的功能,给运行的函数添加了时间日志

    before decorator running,time is Wed Jan 20 19:58:41 2021
    The result is  8
    after decorator running,time is  Wed Jan 20 19:58:42 2021
  • 相关阅读:
    codeforces #330 div2
    codeforces #332 div2
    Codeforces Round #331 (Div. 2)C. Wilbur and Points
    poj 01背包
    zoj 1200 Mining
    nginx反向代理与负载均衡
    springcloud----config分布式配置中心
    springcloud--zuul 网关
    springcloud ----Hystrix熔断器
    docker私有镜像仓库harbor搭建和配置
  • 原文地址:https://www.cnblogs.com/AntonioSu/p/14304832.html
Copyright © 2020-2023  润新知