Discuss / Python / 交作业

交作业

Topic source

关于思考题 beigin callend call 还有既支持 @log() 也支持 @log('exacute') 的个人答案

但是 @log 做不到支持,知道的大神麻烦告知一声,十分感谢

import time, functools

def log(text = 'default'):
    def metric(func):
        @functools.wraps(func)
        def wrapper(*args, **kw):
            print('BEGIN CALL: [%s] : %s executed in %s ms' % (text, func.__name__, time.time()))
            def endLog():
                result = func(*args, **kw)
                print('END CALL: [%s] : %s executed in %s ms' % (text, func.__name__, time.time()))
                return result # 返回函数结果
            return endLog()
        return wrapper
    return metric

@log()
def fast(x, y):
    time.sleep(0.0012)
    return x + y;

@log('not default')
def slow(x, y, z):
    time.sleep(0.1234)
    return x * y * z;

f = fast(11, 22)
s = slow(11, 22, 33)
if f != 33:
    print('fast测试失败!')
if s != 7986:
    print('slow测试失败!')

青铜神裔

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

我的评论里有分析,虽然最后也没个正确答案…… 另外,能请教一下怎么弄代码框吗?

这里涉及到 Markdown 语法,有兴趣的直接点链接去看看或者自己查阅吧,我就直说代码块是怎么弄的

如下输入即可:

``` python 代码 ```

青铜神裔

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

感谢


  • 1

Reply