请有人解释为什么在使用 %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
andrepr(self.vals)
代替吗?