Python中装饰器,迭代器和生成器

Python中的装饰器被用于有切面(AOP)需求的场景,如插入日志、性能测试、事务处理等
测试函数的执行时间:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def test_runtime(func):
def _deco():
start = time.time()
func()
end = time.time()
print 'time:', end-start
return _deco

@test_runtime
def gen():
for i in range(100000000):
pass
print 'AAAAAAAAAAAAAAAA'

gen()