[Python-Dev] str(container) should call str(item), not repr(item)

Steve Holden steve at holdenweb.com
Mon Jul 28 13:24:18 CEST 2008


Sebastian Haase wrote:
> On Mon, Jul 28, 2008 at 10:59 AM,  <skip at pobox.com> wrote:
>>    Ondrej> i.e. the str on list (and tuple and dict) calls repr() on the
>>    Ondrej> elements, instead of str. This really seems to me like a bug.
>>    Ondrej> Because if I wanted the repr() representation, I'd call repr()
>>    Ondrej> on the list/tuple/dict. If I want a nice readable
>>    Ondrej> representation, I call str(). That's the philosophy, no?
>>
>> I think this is the case which calls for the distinction:
>>
>>    >>> str(["1", "2", "3"])
>>    "['1', '2', '3']"
>>    >>> str([1, 2, 3])
>>    '[1, 2, 3]'
>>
>> If the first case did as you suggested you couldn't distinguish it from the
>> second.
>>
> Look at this -- it seems to me that it should work fine....
> ---- Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
>>>> str("qwer")
> 'qwer'
>>>> repr("qwer")
> "'qwer'"
> 
No it doesn't. What's happening in these examples is that the 
interpreter is calling repr() on the expression result - otherwise you 
wouldn't see the quotes:

 >>> str("qwer")
'qwer'
 >>> print str("qwer")
qwer
 >>>

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/



More information about the Python-Dev mailing list