首页 > 文章列表 > python在带参的函数中使用装饰器

python在带参的函数中使用装饰器

Python 函数 装饰器
325 2022-08-07

方法说明

1、如果要包装的函数有参数,需要内嵌包装函数的形参和返回值与原函数相同。

2、装饰函数返回内嵌包装函数对象。

实例

import datetime,time
 
def out(func):
    def inner(*args):
        start = datetime.datetime.now()
        func(*args)
        end = datetime.datetime.now()
        print(end-start)
        print("out and inner")
    return inner
 
@out
def myfunc(*args):
    time.sleep(1)
    print("args is{}".format(args))
 
myfunc("lalalal")

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。