python decorator 基础
# -*- coding: utf-8 -*-
def log_cost_time(func):
def wrapped(*args, **kwargs):
import time
begin = time.time()
try:
return func(*args, **kwargs)
finally:
print 'func %s cost %s' % (func.__name__, time.time() - begin)
return wrapped @log_cost_time
def complex_func(num):
ret = 0
for i in xrange(num):
ret += i * i
return ret
#complex_func = log_cost_time(complex_func) if __name__ == '__main__':
print complex_func(100000)
code snippet 0
@decdef func():pass
- functools.update_wrapper
functools.update_wrapper(wrapper, wrapped[, assigned][, updated])- functools.wraps: update_wrapper的封装
-
This is a convenience function for invoking
partial(update_wrapper,wrapped=wrapped,assigned=assigned,updated=updated)as a function decorator when defining a wrapper function.
简单改改代码:
import functools
def log_cost_time(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
import time
begin = time.time()
try:
return func(*args, **kwargs)
finally:
print 'func %s cost %s' % (func.__name__, time.time() - begin)
return wrapped
def log_cost_time(stream):
def inner_dec(func):
def wrapped(*args, **kwargs):
import time
begin = time.time()
try:
return func(*args, **kwargs)
finally:
stream.write('func %s cost %s \n' % (func.__name__, time.time() - begin))
return wrapped
return inner_dec import sys
@log_cost_time(sys.stdout)
def complex_func(num):
ret = 0
for i in xrange(num):
ret += i * i
return ret if __name__ == '__main__':
print complex_func(100000)
code snippet 1
def Haha(clz):
clz.__str__ = lambda s: "Haha"
return clz @Haha
class Widget(object):
''' class Widget ''' if __name__ == '__main__':
w = Widget()
print w
动态地为某个对象增加额外的责任

由于装饰器模式仅从外部改变组件,因此组件无需对它的装饰有任何了解;也就是说,这些装饰对该组件是透明的。


def catchall(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
try:
return func(*args, **kwargs)
except:
pass
return wrapped
python decorator 基础的更多相关文章
- Python Decorator 和函数式编程
看到一篇翻译不错的文章,原文链接: Python Decorator 和函数式编程
- Python文件基础
===========Python文件基础========= 写,先写在了IO buffer了,所以要及时保存 关闭.关闭会自动保存. file.close() 读取全部文件内容用read,读取一行用 ...
- 3.Python编程语言基础技术框架
3.Python编程语言基础技术框架 3.1查看数据项数据类型 type(name) 3.2查看数据项数据id id(name) 3.3对象引用 备注Python将所有数据存为内存对象 Python中 ...
- Python爬虫基础
前言 Python非常适合用来开发网页爬虫,理由如下: 1.抓取网页本身的接口 相比与其他静态编程语言,如java,c#,c++,python抓取网页文档的接口更简洁:相比其他动态脚本语言,如perl ...
- 小白必看Python视频基础教程
Python的排名从去年开始就借助人工智能持续上升,现在它已经成为了第一名.Python的火热,也带动了工程师们的就业热.可能你也想通过学习加入这个炙手可热的行业,可以看看Python视频基础教程,小 ...
- Python爬虫基础之requests
一.随时随地爬取一个网页下来 怎么爬取网页?对网站开发了解的都知道,浏览器访问Url向服务器发送请求,服务器响应浏览器请求并返回一堆HTML信息,其中包括html标签,css样式,js脚本等.我们之前 ...
- 零基础学Python--------第2章 Python语言基础
第2章 Python语言基础 2.1 Python语法特点 2.11注释 在Python中,通常包括3种类型的注释,分别是单行注释.多行注释和中文编码声明注释. 1.单行注释 在Python中,使用 ...
- Python学习基础笔记(全)
换博客了,还是csdn好一些. Python学习基础笔记 1.Python学习-linux下Python3的安装 2.Python学习-数据类型.运算符.条件语句 3.Python学习-循环语句 4. ...
- Python数据分析基础教程
Python数据分析基础教程(第2版)(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1_FsReTBCaL_PzKhM0o6l0g 提取码:nkhw 复制这段内容后 ...
随机推荐
- JDBC数据源 使用JNDI连接池实现数据库的连接
0.引言 许多Web应用程序需要通过JDBC驱动程序访问数据库,以支持该应用程序所需的功能.Java EE平台规范要求Java EE应用程序服务器为此目的提供一个DataSource实现(即,用于JD ...
- tmux frequently asked questions
tmux frequently asked questions How is tmux different from GNU screen? tmux and GNU screen have ...
- Getting Started With setuptools and setup.py
https://pythonhosted.org/an_example_pypi_project/setuptools.html http://www.ianbicking.org/docs/setu ...
- SICK激光雷达LMS511测量数据说明
帧结构说明 LMS511的官方手册存在几个版本,在<Laser Measurement Systems of the LMS500 Product Family>的英文手册中,对单次(连续 ...
- 2)C语言的基本知识(C自考学习)
字符集 在C语言程序中允许出现的所有基本字符的组合称为C语言的字符集.C语言的字符集就是ASCII字符集.主要包含一下几类: 1)大小写英文字母A~Z,a~z(52个) 2)数字0-9(10个) 3) ...
- 01_Python简介
Python 简介 *为什么学习python http://bbs.fishc.com/thread-35584-1-1.html Python 特点 易于学习:Python有相对较少的关键字,结构简 ...
- 3721:和数-poj
总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个正整数序列,判断其中有多少个数,等于数列中其他两个数的和. 比如,对于数列1 2 3 4, 这个问题的答案就是2, 因为3 = ...
- [特斯拉组件]ios高性能PageController
本文来自于腾讯Bugly公众号(weixinBugly),作者:sparrowchen,未经作者同意,请勿转载,原文地址: http://mp.weixin.qq.com/s/hBgvPBP12IQ1 ...
- I/O复用中的 select poll 和 epoll
I/O复用中的 select poll 和 epoll: 这里有一些不错的资料: I/O多路复用技术之select模型: http://blog.csdn.net/nk_test/article/de ...
- 利用阿里云Centos7建站过程
以下可能不尽详述,如有问题欢迎指出 准备过程:1. 阿里云主机一台2.域名一个 3.github个人帐号开始: 1.以root帐号登录云主机 2.安装apache [root@192 ~]# yum ...