2013-12-08 48 views

回答

1

我猜你的功能签名有问题。虽然您给了它两个参数,但回溯实际上意味着函数需要2 位置 参数。也许你的关键字参数一个不匹配的位置参数名称:

>>> def foo(a, b, c='foo'): 
...  pass 
... 
>>> foo(a="cat", b="bar") 
>>> foo(1, c="bar") 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: foo() takes at least 2 arguments (2 given) 

从技术上讲,它只是2 需要参数。在python3中,你可以有必需的关键字(即非位置)参数。看评论。

+0

你的答案可能对python2有好处,但是在python3关键字参数*可以是必须的:'foo(a,b,*,c)'。错误消息意味着写的是什么:'foo()'至少需要'N'(强制性)参数。 (*这些参数*)给出了'K'()。 – Bakuriu

+1

@Bakuriu - 足够公平,但python 3(在我的机器上至少3.4)给出了更多有用的错误消息:'TypeError:foo()缺少1必需的关键字参数:'c''这不是什么OP获得。 – mgilson

相关问题