编程语言
首页 > 编程语言> > python多参数多功能多个装饰器

python多参数多功能多个装饰器

作者:互联网

下面展示多参数多功能多个装饰器
具体代码如下:

# Author:HY
import time
import math
import sys
def deco(func):
    def wrapper(*args,**kwargs):
        startt = time.time()
        time.sleep(0.00000001)
        func(*args,**kwargs)
        endt = time.time()
        f=str(func)[1:12]
        print("The %s runs %s" % (f,(endt - startt)))
    return wrapper

def jdt(func):
    def wrapper(*args,**kwargs):
        func(*args,**kwargs)
        for i in range(100):
            sys.stdout.write("%s" % i)
            sys.stdout.write("% ")
            sys.stdout.flush()
            time.sleep(0.1)
    return wrapper


def defunc(func):
    startt=time.time()
    func()
    endt=time.time()
    print("The func runs %s"%(endt-startt))
    # return func

@jdt
@deco
def f1(a,b):
    print("This is 周长!")
    print("C=",2*(a+b))

@deco
def f2(r):
    print("This is 面积!")
    pai=math.pi
    print("S=",pai*r*r)

@deco
def f3(a,b,c):
    print("This is 体积!")
    print("V=",a*b*c)

f1(1,2)
f2(1)
f3(3,4,5)

输出结果如下:

This is 周长!
C= 6
The function f1 runs 0.0010447502136230469
0% 1% 2% 3% 4% 5% 6% 7% 8% 9% 10% 11% 12% 13% 14% 15% 16% 17% 18% 19% 20% 21% 22% 23% 24% 25% 26% 27% 28% 29% 30% 31% 32% 33% 34% 35% 36% 37% 38% 39% 40% 41% 42% 43% 44% 45% 46% 47% 48% 49% 50% 51% 52% 53% 54% 55% 56% 57% 58% 59% 60% 61% 62% 63% 64% 65% 66% 67% 68% 69% 70% 71% 72% 73% 74% 75% 76% 77% 78% 79% 80% 81% 82% 83% 84% 85% 86% 87% 88% 89% 90% 91% 92% 93% 94% 95% 96% 97% 98% 99% This is 面积!
S= 3.141592653589793
The function f2 runs 0.0019686222076416016
This is 体积!
V= 60
The function f3 runs 0.00160980224609375

Process finished with exit code 0

标签:runs,deco,python,多功能,func,time,print,装饰,def
来源: https://blog.csdn.net/Hy2424729039/article/details/112172302