[Python-3000] Recursive str
Oleg Broytmann
phd at phd.pp.ru
Fri Apr 11 14:55:22 CEST 2008
On Fri, Apr 11, 2008 at 02:44:34PM +0200, Thomas Wouters wrote:
> 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.
I see. Thank you!
> 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.
str([a, b, c]) currently does a wrong thing if items are non-ascii
strings - calling repr() on them produces '\XXX' escapes instead of
a readable representation.
Oleg.
--
Oleg Broytmann http://phd.pp.ru/ phd at phd.pp.ru
Programmers don't die, they just GOSUB without RETURN.
More information about the Python-3000
mailing list