[Python-3000] str(container) calls repr()
Nick Coghlan
ncoghlan at gmail.com
Tue May 27 11:28:42 CEST 2008
Oleg Broytmann wrote:
> On Tue, May 27, 2008 at 06:13:39PM +1000, Nick Coghlan wrote:
>> str([1, 2, 3]), str(list("123")) and str(["1, 2, 3"]) should all produce
>> distinctive output: calling repr() on container contents achieves this,
>> calling str() does not.
>
> String representation is a special case and *the only* special case, and
> must be handled as a special case.
The problem arises whenever you have two different objects which can
produce the same answer for str(), but different answers for repr().
Strings are the most obvious case, since str(obj) and str(repr(obj))
will always produce the same answer for any object which doesn't have
separate __str__ and __repr__ implementations, but they aren't the only
case (as I endeavoured to show with the Decimal example).
> I don't like this special case to be used
> as a model for all other types. No other type allows usage like list("123").
That's just the way I happened to write it. You can enter it as
str(list(["1", "2", "3"])) if you prefer.
> The decision should be upon the user. In an ideal world str(container)
> calls str() on items, and repr(container) calls repr() on items, so the
> user can choose what [s]he wants. Currently user is just stuck with repr().
That's hardly the case - developers are quite free to iterate over the
container invoking str() on each of the sub-items and building the
output that way.
All I'm really asking for here is for people to identify the use cases
that justify introducing such a potential for ambiguity into the
container implementations.
That has been done to my satisfaction for PEP 3138 (which is why I've
switched from being an opponent of allowing repr() to return arbitrary
Unicode characters to being a supported of the PEP), but I've yet to see
*any* specific use cases for containers invoking str() that wouldn't be
better addressed with an application or library specific display loop.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-3000
mailing list