@deco2 @deco1 def func(arg1,arg2...): pass
@deco def func(): pass
@deco(deco_args) def func(): pass
@deco2(arg1,arg2) @deco1 def func(): pass
#!/usr/bin/env python
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(2):
sleep(1)
foo()
[root@python 420]# python deco.py [Tue Apr 19 16:15:01 2016] foo() called [Tue Apr 19 16:15:06 2016] foo() called [Tue Apr 19 16:15:07 2016] foo() called
#!/usr/bin/env python
def counter(start_at=0):
count = [start_at]
def incr():
count[0] += 1
return count[0]
return incr
count= counter(5)
print count()
print count()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有