首页 > 编程语言 > 详细

Python面向切面编程-语法层面和functools模块

时间:2017-06-28 21:34:47      阅读:254      评论:0      收藏:0      [点我收藏+]

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') 


Python面向切面编程-语法层面和functools模块

原文:http://www.cnblogs.com/mthoutai/p/7091477.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!