Discuss / Python / 提问

提问

Topic source

shenglxf

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

看了很多别人写的代码,然后产生了一个疑问

import functools
def log(text):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kw):
            if not isinstance(text, (str, int, float)):
                text = 'call'
            print('Begin', text, func.__name__)
            result = func(*args, **kw)
            print('End', text, func.__name__)
            return result
        return wrapper
    if isinstance(text, (str, int, float)):
        return decorator
    else:
        return decorator(text)

为什么会出现text引用之前未指定的错误呢


  • 1

Reply