Discuss / Python / 我不是说你写错了,我是说在座的各位都写错了!

我不是说你写错了,我是说在座的各位都写错了!

Topic source

Dreamer_DK

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

当然我也写错了。但是不会写对的啊!!!!!! 真的是求教了!! 代码如下,判断就不写了,只是添加一个if...else语句。 就作业第一题:

import functools

def log(func):
    @functools.wraps(func)
    def wrapper(*args,**kw):
        print ('%s %s' % ('begin call:',func.__name__))
        f = func(*args,**kw)
        print ('%s %s' % ('end call:',func.__name__),'\n')
        return f
    return wrapper

@log
def test(x,y):
        return x+y

print (test(3,5))

@log
def test1():
    print ('代码执行')

test1()

我们执行就可以看到,print(test())以后,test()运行是把begin和end都执行完了才计算的值。也就是说其实是输出begin和end以后再执行的函数。 第二个test1()执行起来倒是正常的,应该是因为函数里面只有print()输出函数立即执行了吧。

等待解答与改进。


  • 1

Reply