Discuss / Python / 展示一下自己的代码 ,不足请指出

展示一下自己的代码 ,不足请指出

Topic source

90qn

#1 Created at ... [Delete] [Delete and Lock User]

第一题:

def decorator(func):
    def warps(*args,**kw):
        print("begin call")
        a = func(*args,**kw)
        print("end call")
        return a
    return warps
@decorator
def now():
    print("haha")
now()

第二题 装饰器和函数代码

def log(text):
    def decorator(func):
        def warps(*args,**kw):
            print("begin call")
            print("%s : %s" % (text,func.__name__))
            a = func(*args,**kw)
            print("end call")
            return a
        return warps
    if isinstance(text,str):
        return decorator
    func = text
    text = "excute"
    return decorator(func)
@log("excited")
def now():
    print("haha")
now()

第二种

def log(text):
    def decorator(func):
        def warps(*args,**kw):
            print("begin call")
            print("%s : %s" % (text,func.__name__))
            a = func(*args,**kw)
            print("end call")
            return a
        return warps
    if isinstance(text,str):
        return decorator
    func = text
    text = "excute"
    return decorator(func)
@log
def now():
    print("haha")
now()

请问为什么要写 text = "excute" 感觉text 已经没有用了啊

求教 func = text text = "excute" return decorator(func) 是什么意思


  • 1

Reply