• 技术文章 >Python技术 >Python基础教程

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

    小妮浅浅小妮浅浅2021-08-19 09:29:48原创3940

    方法说明

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

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

    实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    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")

    以上就是python在带参函数中使用装饰器的方法,希望对大家有所帮助。更多Python学习指路:python基础教程

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

    专题推荐:python 函数 装饰器
    上一篇:python break和continue的比较 下一篇:python清洗文件数据的方法

    相关文章推荐

    • python异常中else的使用• 如何用Python编写客户端程序• Python中tail -f如何实现• python如何实现Stack• python类如何自定义实例化• python tqdm是什么• python异常处理的常见错误• python logging日志的禁用• python for循环遍历位置的查找• python if判断的使用格式• python break和continue的比较

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网