python带参数和不带参数装饰器,,http://blo
python带参数和不带参数装饰器,,http://blo
http://blog.csdn.net/pipisorry/article/details/41902599
对于无参数的装饰器,其装饰器函数的参数是要被装饰的函数对象名;
对于有参数的装饰器在调用时使用的是应用的参数,@timeStumpFunc_args(argv)的argv,已不再是要被装饰的函数对象名,所以必须在内部再定义一个函数getfunc()来接收要被装饰的函数对象。
#!/usr/bin/env python# coding=gbk"""__title__ = '带参数和不带参数的timeStump'__author__ = 'pi'__mtime__ = '2014.12.12'"""from time import ctimedef timeStumpFunc(func): """time stump decorator of func 不带参数的时间戳函数""" def wrappedFunc(*nkw): print("start_time %s" % ctime()) func(*nkw) print("end_time %s" % ctime()) return wrappedFuncdef timeStumpFunc_args(args): """time stump decorator of func 不带参数的时间戳函数""" print "timeStump for function %s" % args def getFunc(func): def wrappedFunc(*nkw): print("start_time %s" % ctime()) func(*nkw) print("end_time %s" % ctime()) return wrappedFunc return getFunc@timeStumpFunc# @timeStumpFunc_args('do_sth')def do_sth(*nkw): print "%s" % nkwif __name__ == '__main__': do_sth('i you love')
from:http://blog.csdn.net/pipisorry/article/details/41902599
ref:Python装饰器:简单装饰,带参数装饰与类装饰器
http://outofmemory.cn/code-snippet/1107/python-achieve-carry-parameter-decorator
python带参数和不带参数装饰器
相关内容
- Python 之 ImportError: No module named ***,,如果想使用非当前
- python中入口函数的用法,,入口函数的用法 先
- Python2和Python3的区别(未完待续。。。。),,1、print函
- Python基本数据类型(二),,数字类型:数字的定义
- 小甲鱼python基础教程飞机大战源码及素材,,百度了半天
- python socket 端口测试,,#coding:ut
- Python图像处理(1):替换OpenCV,,快乐虾http://
- python异常处理,,异常处理捕捉异常可以
- python面向对象,类,,类:抽象的概念;是属
- python学习——编码,,为了将各种不同的语言
评论关闭