Python 装饰器

1. 带有提示信息、计算运行时长的函数装饰器


import time

def timeit(msg):
    def out_wraper(func):
        def in_wraper(*args, **kargs):
            a = time.time()
            res = func(*args, **kargs)
            b = time.time()
            print 'msg: %s' % msg
            print u'%s run %s seconds...' % (func.__name__, b - a)
            return res
        return in_wraper
    return out_wraper

@timeit('hahaha')
def f():
    x = [x for x in xrange(10000)]
    print x[:201:2]

if __name__ == '__main__':
    f()



  • 待补充

github: https://github.com/buptxiaomiao/python_trick/blob/master/deco.py

你可能感兴趣的:(Python 装饰器)