[Python-3000] Recursive str

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Apr 17 03:36:54 CEST 2008


Oleg Broytmann wrote:
> Do I understand it right that str(objects) calls repr() on items to
> properly quote strings? (str([1, '1']) must give "[1, '1']" as the result).
> Is it the only reason?

In the case of strings, yes. More generally, there
can be any kind of object in the list, and repr(x)
is more likely to give an unambiguous idea of what
x is than str(x) when it's embedded in a comma-
separated list.

Python has no way of guessing the most appropriate
way to display your list of objects when you use
str(), so it doesn't try. You have to tell it by
writing code to do what you want.

-- 
Greg


More information about the Python-3000 mailing list