Python中实现装饰模式的三种方式
功能目标
编写一个可以打印被装饰函数名称、执行时间、内存地址得装饰器
前置依赖包
import time
import functools
from decorator import decorator
基于普通的函数嵌套
> def log1(fn):
def _wrapper(*args, **kwargs):
start = time.clock()
result = fn(*args, **kwargs)
print("%s is invoked with time consumed: %s seconds at address %s" % (fn.__name__, str(time.time() - start), id(fn)))
return result
return _wrapper
基于@decorator
@decorator
def log(f, *args, **kwargs):
start = time.time()
result = f(*args, **kwargs)
print("%s is invoked with time consumed: %s at address %s" % (f.__name__, str(time.time() - start), id(f)))
return result
使用@functools
def log2(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
start = time.clock()
result = f(*args, **kwargs)
print("%s is invoked with time consumed: %s seconds at address %s" % (f.__name__, str(time.time() - start), id(f)))
return result
return wrapper
测试
@log2
def f11(x, y):
return x**y
result = f11(2,3)
print("result:" + str(result))
切换到不同的实现中,他们的效果是相同的。
Python中实现装饰模式的三种方式的更多相关文章
- Python中字符串拼接的三种方式
在Python中,我们经常会遇到字符串的拼接问题,在这里我总结了三种字符串的拼接方式: 1.使用加号(+)号进行拼接 加号(+)号拼接是我第一次学习Python常用的方法,我们只需要把我们要加 ...
- 【转】Python中执行cmd的三种方式
原文链接:http://blog.csdn.net/menglei8625/article/details/7494094 目前我使用到的python中执行cmd的方式有三种: 1. 使用os.sys ...
- python中反转列表的三种方式
1.内建函数reversed() li =[1, 2, 3, 4, 5, 6] a = list(reversed(li)) print (a) 注意:reversed()函数返回的是一个迭代器,而不 ...
- python中字符串连接的三种方式
1.字符串之间连接 'aa' 'bb' 可以中间为空格 或者什么都没有. 那么输出都是两者之间紧密相连. 2.字符串+数字 'aa' +90 这样会报错,因为不同类型不能相加, 可以用 'aa',90 ...
- Python实现微信支付(三种方式)
Python实现微信支付(三种方式) 关注公众号"轻松学编程"了解更多. 如果需要python SDk源码,可以加我微信[1257309054] 在文末有二维码. 一.准备环境 1 ...
- android中解析文件的三种方式
android中解析文件的三种方式 好久没有动手写点东西了,最近在研究android的相关技术,现在就android中解析文件的三种方式做以下总结.其主要有:SAX(Simple API fo ...
- Struts中的数据处理的三种方式
Struts中的数据处理的三种方式: public class DataAction extends ActionSupport{ @Override public String execute() ...
- JS中事件绑定的三种方式
以下是搜集的在JS中事件绑定的三种方式. 1. HTML onclick attribute <button type="button" id="upl ...
- JavaScript 中事件绑定的三种方式
以下是在 JS 中事件绑定的三种方式. 1. HTML onclick attribute <button type="button" id="uplo ...
随机推荐
- ES集群重启
操作步骤: 1. Disable shard allocation curl -XPUT 'localhost:9200/_cluster/settings?pretty' -d '{ " ...
- SSDB 使用笔记
1. SSDB中scan key_start key_end limit ,key_start 和 key_end 是指字母的顺序,不是数字. 2. 进入客户端:./ssdb-cli -p 8888
- ros使用时的注意事项&技巧
1.rosrun package-name executable-name 比如 rosrun turtlesim turtlesim_node 2.一旦启动roscore后,便可以运行ROS程序了. ...
- Microsoft.VisualStudio.Web.PageInspector.Loader
未能加载文件或程序集"Microsoft.VisualStudio.Web.PageInspector.Loader, Version=1.0.0.0, Culture=neutral, P ...
- cookie 与 session 的区别详解
[转]cookie 与session 的区别详解 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录下来.当 ...
- centos7下apache+tomcat整合
前提 在系统中已经安装好了jdk.tomcat.apache #本人博客中jdk安装连接 http://www.cnblogs.com/xhkj/p/6545111.html #本人博客中tomcat ...
- MySql 查询数据库中所有表名以及对比分布式库中字段和表的不同
查询数据库中所有表名select table_name from information_schema.tables where table_schema='数据库名' and table_type= ...
- 选择使用Spring框架的原因(Spring框架为企业级开发带来的好处有哪些)
- 你知道uwsgi???
第一步,打开https://www.google.com.hk 第二步,输入how to restart uwsgi
- LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List
题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...