Python面向切面编程-语法层面和functools模块
来源:程序员人生 发布时间:2014-09-15 14:22:47 阅读次数:3244次
1,Python语法层面对面向切面编程的支持(方法名装饰后改变为log)
__author__ = 'Administrator'
import time
def log(func):
def wrapper(*args):
start = time.time()
func(args)
end =time.time()
print 'func used time is :', end - start
return wrapper
@log
def reg(args):
print 'welcome %s ' %(args[0])
reg('joeyon','123456')
2,functools模块对面向切面的支持(方法名装饰后不改变)
import time
from functools import wraps
def log(func):
@wraps(func)
def wrapper(arg1,arg2):
start = time.time()
func(arg1,arg2)
end =time.time()
print 'func used time is :', end - start
return wrapper
@log
def reg(username,pwd):
print 'welcome %s ' %(username)
reg('joeyon','123456')
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠