请问为什么我在使用 %r 与元组时会出现以下错误?
>>> repr((1,2))
'(1, 2)'
>>> class Foo(object):
... def __init__(self,vals):
... self.vals=vals
... def __repr__(self):
... return "Foo(%r)" % self.vals
...
>>> foo = Foo((1,2))
>>> foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in __repr__
TypeError: not all arguments converted during string formatting
如何适当地打印 __repr__
?我应该使用 %s
和 repr(self.vals)
吗?