返回顶部

收藏

python 装饰器体验

更多
from time import ctime,sleep
def tsfunc(fun):
    def weappedFunc(*args,**kwargs):
        print(ctime(),fun.__name__,"called")
        return fun(*args,**kwargs)
    return weappedFunc
def tsfunc2(arg):
    def weappendFunc(fun):
        def weappendFunc2(*args,**kwargs):
            print(ctime(),fun.__name__,"called",arg)
            fun(*args,**kwargs)
        return weappendFunc2
    return weappendFunc

@tsfunc
def foo(a,b):
    print("foo calling:","a=",a,"b=",b)
    pass
@tsfunc
def foo2(a,b,c):
    print("foo2 calling:","a=",a,"b=",b,"c=",c)
@tsfunc
def foo3():
    print("foo3 called")

@tsfunc2("hehhe")
def foo4():
    print("foo4 called")
foo(1,2)
foo2(1,2,3)
foo3()
foo4()
#该片段来自于http://outofmemory.cn

标签:python,基础

收藏

0人收藏

支持

0

反对

0