Discuss / Python / 针对小结中的讨论写了个代码,自行体会

针对小结中的讨论写了个代码,自行体会

Topic source

coLBooy

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

import functools

def log(text=None):

    def decorator(func):

        @functools.wraps(func)

        def wrapper(*args, **kw):

            if not text is None:

                print(text)

            print('begin call')

            result=func(*args, **kw)

            print('end call')

            return result

        return wrapper

    return decorator

@log('hello')

def now(x,y):

    print(x+y)

    return x+y

print(now(1,2))

asterwyx

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

我想问一下,有没有什么办法不在原函数中添加print(x + y)就可以实现在出结果后才打印endcall呢

金钟铉

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

你这样不能@log这样写


  • 1

Reply