博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
day20 Python 装饰器
阅读量:7036 次
发布时间:2019-06-28

本文共 732 字,大约阅读时间需要 2 分钟。

装饰器:本质就是函数,为其他函数添加附加功能,一个原则是不修改函数的源代码,另外一个原则是不修改被修饰函数的调用功能

装饰器=高阶函数+函数嵌套+闭包

前戏

import timedef cal(l):    start_time=time.time()    res=0    for i in l:        time.sleep(0.1)        res+=i    stop_time = time.time()    print('函数的运行时间是%s' %(stop_time-start_time))    return resprint(cal(range(100)))def index():    passdef home():    pass

 装饰器

import timedef timmer(func):    def warpper(*args,**kwargs):        start_time=time.time()        func()        stop_time=time.time()        print("the func run time is %s" % (stop_time-start_time))    return warpper@timmerdef test1():    time.sleep(3)    print("in the test1")test1()结果:in the test1the func run time is 3.0026323795318604

 

转载于:https://www.cnblogs.com/charon2/p/10388961.html

你可能感兴趣的文章
UniDAC使用教程(一):连接到数据库
查看>>
PHP 连接 MySQL
查看>>
314安装与网络配置预习+笔记
查看>>
python项目实战:免费下载kugou任意付费音乐
查看>>
消息中间件-activeMQ
查看>>
SQL SERVER2008数据库常识
查看>>
mac outlook 自动回复
查看>>
横屏竖屏
查看>>
Zabbix监控MySQL
查看>>
OSChina 周三乱弹 ——祖传的程序员?????
查看>>
OSChina 周五乱弹 —— 埃塞俄比亚的远房大表姐
查看>>
大华设备扫描工具
查看>>
twisted的异步库汇总-- mysql,redis,mongo,zmq,sockjs等
查看>>
Python3 基于asyncio的新闻爬虫思路
查看>>
af3.0学习使用和理解
查看>>
Linux vmstat命令实战详解
查看>>
输入字符串,输出字符串所有组合
查看>>
Python中 字典排序、列表排序
查看>>
ubuntu12.04 安装vnc
查看>>
我的友情链接
查看>>