python基本装饰器

本文详细解析了Python装饰器的执行顺序,包括带有参数的装饰器如何工作,以及装饰器内部闭包函数的执行过程。通过具体示例,展示了装饰器在函数调用前后如何记录日志和运行时间。
#基本代码:
    import time

    def decoratorrunlog_args(logval):
            print("logval is runing"+str(logval))
            def decoratorrunlog(func):
                    print("outerlog is runing")
                    def inner(*args,**kwargs):
                            print("innerlog is runing")
                            stime = time.time()
                            time.sleep(1)
                            res = func(*args,**kwargs)
                            etime = time.time()
                            print(etime - stime)
                            print("innerlog is stoping")
                            return  res
                    print("outerlog is stoping")
                    return  inner
            print("logval is stoping")
            return  decoratorrunlog

    def decoratortuntime_args(timeval):
            print("timeval is runing:"+str(timeval))
            def decoratorruntime(func):
                    print("outer is runing")
                    def inner(*args,**kwargs):
                            print("inner is runing")
                            stime = time.time()
                            time.sleep(1)
                            res = func(*args,**kwargs)
                            etime = time.time()
                            print(etime - stime)
                            print("inner is stoping")
                            return  res
                    print("outer is stoping")
                    return  inner
            print("timeval is stoping")
            return  decoratorruntime

    @decoratorrunlog_args("argsonelog")
    @decoratortuntime_args("argstwotime")
    def funcone(pname):
            print("this is a basic function")
            return  pname

    res = funcone("lily")
    print(res)

#执行结果
       logval is runingargsonelog
        logval is stoping
        timeval is runing:argstwotime
        timeval is stoping
        outer is runing
        outer is stoping
        outerlog is runing
        outerlog is stoping
        innerlog is runing
        inner is runing
        this is a basic function
        1.000281810760498
        inner is stoping
        2.000582218170166
        innerlog is stoping
        lily

#图示

python基本装饰器

#说明:
    图中:1.1,1.2,2.1,2.2 中,  "."之前的数字表示执行的步骤,之后的数字表示多个装饰器的数字标识;

    #运行顺序
        基本是按照代码编写顺序执行
        第一步:1.1、1.2: 执行带参数的装饰器本身,
        第二步:2.2、2.1:执行装饰器
        第三步:3.1、3.2:执行装饰器内部的闭包函数
        第四步:func:执行装饰器装饰的函数本身
        第五步:5.2、5.1:执行闭包中func后面的部分

        装饰器执行的顺序:按照其在所装饰的函数中放置的顺序来执行。首先从最上面的装饰器开始按顺序执行到函数位置,然后在从函数位置向上执行到最上面的装饰器,再接着向下执行,如此反复直到装饰器最内部的函数执行到被装饰的函数本身之后才开始执行被装饰的函数。顺序类似于:↓↑↓↑

转载于:https://blog.51cto.com/11089980/2355409

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值