[Python-3000] Recursive str (was: Types and classes)

Thomas Wouters thomas at python.org
Fri Apr 11 14:44:34 CEST 2008


On Wed, Apr 9, 2008 at 6:30 PM, Oleg Broytmann <phd at phd.pp.ru> wrote:

> On Wed, Apr 09, 2008 at 09:20:56AM -0700, Guido van Rossum wrote:
> > We
> > already have repr() if you want unambiguous output; str() is for
> > pretty output.
>
>   BTW, does Python 3000 fix the problem that str(container) calls repr()
> instead of str() for elements in the container?
>

No, because there is no sensible way to fix it. If a container defines
__str__, it can do whatever it wants with items inside itself. If the
container doesn't define __str__ (or defines it as an alias to __repr__),
then __repr__ will be used, and the only sensible thing to do is call repr()
on the elements inside it. If you want containers to have a 'prettier'
format when passed to str(), give them a __str__ that does the pretty thing.
Me, I don't see the point of having a 'pretty' format for lists that is
ambiguous. If I want to print a list, 'repr' does what I expect. Or, I loop
over the list and print each element how I expect it to print. I don't see
the value in str(['1', 1, '1, [1]', '1]', '\n[1']) giving hard to understand
output.

-- 
Thomas Wouters <thomas at python.org>

Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20080411/65b0db3e/attachment.htm 


More information about the Python-3000 mailing list