python中的装饰函数
在面向对象(OOP)的设计模式中,decorator被称为装饰模式。OOP的装饰模式需要通过继承和组合来实现,而Python除了能支持OOP的decorator外,直接从语法层次支持decorator。Python的decorator可以用函数实现,也可以用类实现。
decorator可以增强函数的功能,定义起来虽然有点复杂,但使用起来非常灵活和方便。
请编写一个decorator,能在函数调用的前后打印出'begin call'和'end call'的日志。
再思考一下能否写出一个@log的decorator,使它既支持:
@log
def f():
pass
又支持:
@log('execute')
def f():
pass
#heelo.py
__author__ = 'Administrator'
import functools
def log(text=None):
def de(func):
@functools.wraps(func)
def first(*args,**kw):
if text :
print "begin call",func.__name__,'input is ',text
else:
print "begin call",func.__name__
rs= func(*args,**kw)
print 'end call'
return rs
return first return de @log()
def now():
print 'I\'m a boy ' now()
print now.__name__
数学原形

-------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------

#!/usr/bin/env python
from time import ctime,sleep
def tsfunc(func):
def wrappedFunc(*args,**kwargs):
print '[%s] %s () called ' % (ctime(), func.__name__)
return func
return wrappedFunc @tsfunc
def foo():
pass foo()
sleep(4)
for i in range(2):
sleep(1)
foo()
输出:
[Tue Jul 14 20:49:18 2015] foo () called
[Tue Jul 14 20:49:23 2015] foo () called
[Tue Jul 14 20:49:24 2015] foo () called
python中的装饰函数的更多相关文章
- 简单说明Python中的装饰器的用法
简单说明Python中的装饰器的用法 这篇文章主要简单说明了Python中的装饰器的用法,装饰器在Python的进阶学习中非常重要,示例代码基于Python2.x,需要的朋友可以参考下 装饰器对与 ...
- 浅析python中的装饰器decorator
最近学习python,其中decorator比较难理解,遂写一篇来总结供后续查阅. 定义一个函数,想在运行时动态的改变函数的功能,又不想改变函数本身的代码,可以使用高阶函数(可以使用函数作为参数) 装 ...
- Python 中实现装饰器时使用 @functools.wraps 的理由
Python 中使用装饰器对在运行期对函数进行一些外部功能的扩展.但是在使用过程中,由于装饰器的加入导致解释器认为函数本身发生了改变,在某些情况下——比如测试时——会导致一些问题.Python 通过 ...
- 写python中的装饰器
python中的装饰器主要用于在已有函数实现功能前附加需要输出的信息,下面将用实例展示我如何写装饰器. 首先分别尝试写装饰器装饰一个无参函数和一个有参函数(被装饰函数仅输出,无返回值情况下) def ...
- python中的装饰器decorator
python中的装饰器 装饰器是为了解决以下描述的问题而产生的方法 我们在已有的函数代码的基础上,想要动态的为这个函数增加功能而又不改变原函数的代码 例如有三个函数: def f1(x): retur ...
- python --- Python中的callable 函数
python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...
- python中使用zip函数出现<zip object at 0x02A9E418>
在Python中使用zip函数,出现<zip object at 0x02A9E418>错误的原因是,你是用的是python2点多的版本,python3.0对python做了改动 zip方 ...
- [转载]python中multiprocessing.pool函数介绍
原文地址:http://blog.sina.com.cn/s/blog_5fa432b40101kwpi.html 作者:龙峰 摘自:http://hi.baidu.com/xjtukanif/blo ...
- Python 中的isinstance函数
解释: Python 中的isinstance函数,isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo) 如果参数object是cla ...
随机推荐
- js 列表选择
首选定义数组,然后进行操作时遍历数组获取选中值 function getSelect(userId) { //var userId = userCheckBox.value; //标记删除还是添加 v ...
- json的使用(JObect,JsonData,JArray)
JObect与JsonData的区别 JsonData是JObect的简化版本 2.转换过程容易出的bug JsonData:当里面存在null就无法转换 解决方案: JsonData teamjd ...
- 小白学习之Code First(一)
1.根据自己的理解,Code First :通过实体类和相关配置生成对应的数据库,实现实体和数据库的映射关系,或通过实体类和相关配置与已经生成的实体与已经存在的数据库搭建映射关系 例: 实体类:Stu ...
- Cheatsheet: 2017 07.01 ~ 07.31
Other 8 Key Application Performance Metrics & How to Measure Them The Code Review: The Most Impo ...
- 三:SpringCloud-Ribbon
六:Ribbon负载均衡 1. 概述 1.1 是什么 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套==客户端 负载均衡==的工具. 简单的说,Ribbon是Net ...
- C#操作XMl文件(2):使用XmlReader和XmlWriter实现读取和写入
这次使用操作Xml较为常用的方法:使用XMlreader和Xmlwriter 1:读取xml文件的数学和元素 XmlReaderSettings settings = new XmlReaderSet ...
- Java内存区域与虚拟机类加载机制
一.Java运行时数据区域 1.程序计数器 “线程私有”的内存,是一个较小的内存空间,它可以看做当前线程所执行的字节码的行号指示器.Java虚拟机规范中唯一一个没有OutOfMemoryError情况 ...
- Thymeleaf学习记录(8)--表达式基本对象
基础对象 #ctx:上下文对象 /* * ====================================================================== * See ja ...
- Zookeeper JAVA API的使用
0. 前言 zookeeper安装及使用 http://www.cnblogs.com/rocky-fang/p/7880309.html 1. 开发环境配置 1.1 idea创建一个maven工程 ...
- 【js编程艺术】 之有用的函数
学习js的过程中有几个有用的函数. //添加事件函数 function addLoadEvent(func) { var oldonload = window.onload; if(typeof wi ...