Python装饰器:TypeError:函数需要1个位置参数但是2个被赋予

时间:2017-03-03 08:00:56

标签: python python-3.x decorator python-decorators

我试图在Python中测试装饰器的实用程序。当我写下面的代码时,出现错误:

TypeError: fizz_buzz_or_number() takes 1 positional argument but 2 were given

我首先将函数log_calls(fn)定义为

def log_calls(fn):
    ''' Wraps fn in a function named "inner" that writes
    the arguments and return value to logfile.log '''
    def inner(*args, **kwargs):
        # Call the function with the received arguments and
        # keyword arguments, storing the return value
        out = fn(args, kwargs)

        # Write a line with the function name, its
        # arguments, and its return value to the log file
        with open('logfile.log', 'a') as logfile:
            logfile.write(
               '%s called with args %s and kwargs %s, returning %s\n' %
                (fn.__name__,  args, kwargs, out))

        # Return the return value
        return out
    return inner

之后,我使用log_calls来装饰另一个函数:

@log_calls
def fizz_buzz_or_number(i):
    ''' Return "fizz" if i is divisible by 3, "buzz" if by
        5, and "fizzbuzz" if both; otherwise, return i. '''
    if i % 15 == 0:
        return 'fizzbuzz'
    elif i % 3 == 0:
        return 'fizz'
    elif i % 5 == 0:
        return 'buzz'
    else:
        return i

当我运行以下代码时

for i in range(1, 31):
    print(fizz_buzz_or_number(i))

错误TypeError: fizz_buzz_or_number() takes 1 positional argument but 2 were given来了。

我不知道这个装饰器有什么问题,以及如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

你在这里传递2个参数到你的装饰函数:

out = fn(args, kwargs)

如果您想将args元组和kwargs字典应用为变量参数,请回显函数签名语法,因此请再次使用***

out = fn(*args, **kwargs)

请参阅Call expressions reference documentation

  

如果函数调用中出现语法*expression,则表达式必须求值为iterable。来自这些迭代的元素被视为它们是附加的位置参数。

     

[...]

     

如果语法**expression出现在函数调用中,则表达式必须求值为映射,其内容将被视为附加关键字参数。