confused about __str__ vs. __repr__

Tino Wildenhain tino at wildenhain.de
Thu Dec 18 09:36:01 EST 2008


Neal Becker wrote:
...
>>> So if __str__ is "meant for human eyes", then why isn't print using it!
>> it is:
>>
>>  > print x
>> str
>>
>> but dict just uses repr() for all its childs to print.
>>
>> T.
> That makes no sense to me.  If I call 'print' on a container, why wouldn't it recursively  print on the contained objects?  Since print means call str, printing a container should recursively call str on the objects.

Every class is free on how to best implement __str__, you will find
the same behavior on tuple and list as well.

Maybe its discussable to change the implementation sensibly, best if you
would come with a proposal? Perhaps pprint.pprint is a starting point?

Regards
Tino
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3241 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20081218/5f557372/attachment-0001.bin>


More information about the Python-list mailing list