[Python-3000] str(container) calls repr()

Nick Coghlan ncoghlan at gmail.com
Tue May 27 12:27:21 CEST 2008


Oleg Broytmann wrote:
> On Tue, May 27, 2008 at 07:28:42PM +1000, Nick Coghlan wrote:
>> The problem arises whenever you have two different objects which can 
>> produce the same answer for str(), but different answers for repr(). 
> 
>    Aside strings itself, the only example of such objects I can imagine is
> numbers (ints, floats and decimals). str(12) is the same as str('12') and
> the same as str(Decimal('1')), but that's all.

Those are the only examples I can think of in the standard library, but 
who knows what user code is doing. We shouldn't break that without 
compelling use cases.

>> 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.
> 
>    Why do you afraid of ambiguity so much? str() is supposed to produce
> pretty output, not necessary non-ambiguous, tight? And if a user wants
> non-ambiguity [s]he will use repr().

Agreed, but I see the fact that the 'pretty' representation of a 
container is also unambiguous as a feature rather than a bug (note that 
even the pprint pretty printing module uses repr() for container contents).

*shrug* Make the case in a PEP for str() of the standard containers to 
recurse using str() instead of repr() and get enough people to agree 
that it's a good idea and I'll shut up about it. Until that happens, I'd 
like people to stop claiming that the current behaviour is a bug in any 
way shape or form.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list