confused about __str__ vs. __repr__

Neal Becker ndbecker2 at gmail.com
Thu Dec 18 09:51:01 EST 2008


Tino Wildenhain wrote:

> 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

First, I'd like to know if there is a rationale for the current design.  Am I correct in thinking this is a defect?




More information about the Python-list mailing list