12步轻松搞定python装饰器(Python3.5装饰器典型案例分析)
类别:脚本大全 浏览量:533
时间:2021-10-09 00:18:54 12步轻松搞定python装饰器
Python3.5装饰器典型案例分析本文实例讲述了python3.5装饰器。分享给大家供大家参考,具体如下:
|
#!/usr/bin/env python # -*- coding:utf-8 -*- # author:zhengzhengliu #高阶函数+嵌套函数==>装饰器 import time def timer(func): #timer(test1)-->func=test1 def decor(): start_time = time.time() func() #run test1 stop_time = time.time() print ( "the run time of func is %s" % (stop_time - start_time)) return decor @timer #test1 = timer(test1) def test1(): time.sleep( 3 ) print ( "in the test1" ) @timer #test2 = timer(test2) def test2(): time.sleep( 3 ) print ( "in the test2" ) print (timer(test1)) #打印deco的地址 #test1 = timer(test1) #test2 = timer(test2) test1() #-->执行decor test2() |
运行结果:
<function timer.<locals>.decor at 0x00b720c0>
in the test1
the run time of func is 3.000171661376953
in the test2
the run time of func is 3.000171661376953
1、装饰器修饰有参数函数
|
#高阶函数+嵌套函数==>装饰器 import time def timer(func): #timer(test1)-->func=test1 def decor(arg1,arg2): start_time = time.time() func(arg1,arg2) #run test2 stop_time = time.time() print ( "the run time of func is %s" % (stop_time - start_time)) return decor @timer #test2 = timer(test2) = decor test2(name)==>decor(name) def test2(name,age): print ( "test2:" ,name,age) test2( "liu" , 23 ) |
运行结果 :
test2: liu 23
the run time of func is 0.0
2、装饰器修饰多个函数,有的函数带参数,有的函数不带参数的情况(采用参数组)
|
#高阶函数+嵌套函数==>装饰器 import time def timer(func): #timer(test1)-->func=test1 def decor( * args, * * kwargs): start_time = time.time() func( * args, * * kwargs) #run test1 stop_time = time.time() print ( "the run time of func is %s" % (stop_time - start_time)) return decor @timer #test1 = timer(test1) def test1(): time.sleep( 3 ) print ( "in the test1" ) @timer #test2 = timer(test2) = decor test2(name)==>decor(name) def test2(name,age): time.sleep( 1 ) print ( "test2:" ,name,age) #test1 = timer(test1) #test2 = timer(test2) test1() #-->执行decor test2( "liu" , 23 ) |
运行结果:
in the test1
the run time of func is 3.0036065578460693
test2: liu 23
the run time of func is 1.0084023475646973
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/loveliuzz/article/details/77878019
您可能感兴趣
- python装饰器初学者教程(Python3.5装饰器原理及应用实例详解)
- python装饰器怎么编写(Python函数装饰器常见使用方法实例详解)
- python装饰器使用说明(详解Python装饰器)
- 12步轻松搞定python装饰器(Python3.5装饰器典型案例分析)
- python装饰器怎么设置(深入了解和应用Python 装饰器 @decorator)
- python基础教学之125 装饰器简介(python3 property装饰器实现原理与用法示例)
- python装饰器语法与应用(python装饰器简介---这一篇也许就够了推荐)
- python 装饰器模式(python重试装饰器的简单实现方法)
- 泰国人妖(变性手术生殖器要割掉吗)
- 泰国美女(泰国人妖和女性如何区分)
- 泰国旅游业怎么样(泰国的旅游产业)
- 越南新娘(越南新娘婚介网站)
- 越南新娘(越南新娘骗婚套路流程)
- 菲律宾游学(菲律宾游学中介机构)
热门推荐
- html5能取数据库吗(HTML5 客户端数据库简易使用:IndexedDB)
- dockerfile自定义镜像图解(Docker images导出和导入操作)
- sqlserver表分区步骤(行转列之SQL SERVER PIVOT与用法详解)
- mysql中查询数据合并(Mysql合并结果接横向拼接字段的实现步骤)
- python弹跳小球(python GUI实现小球满屏乱跑效果)
- python在txt指定行添加文本(Python修改文件往指定行插入内容的实例)
- python将对象转换成json(python对象与json相互转换的方法)
- 阿里云服务器端口开放(阿里云服务器Windows实例公网网络异常的解决方法)
- php中如何使用缓存(PHP使用OB缓存实现静态化功能示例)
- php怎么设置统计代码(php简单计算权重的方法示例适合抽奖类应用)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8