您的位置:首页 > 编程语言 > Python开发

python核心编程-函数-装饰器

2015-12-01 23:48 716 查看
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from time import ctime,sleep

def tsfunc(func):
'''装饰器'''
def wrappedFunc():
print '[%s] %s() called' % (ctime(),func.__name__)
return func()
return wrappedFunc

@tsfunc
def foo():
pass

foo()
sleep(4)

for i in range(3):
sleep(1)
foo()


输出

D:\Python27\test>func5.py
[Tue Dec 01 22:29:50 2015] foo() called
[Tue Dec 01 22:29:55 2015] foo() called
[Tue Dec 01 22:29:56 2015] foo() called
[Tue Dec 01 22:29:57 2015] foo() called

D:\Python27\test>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: