FAQ: __str__ vs __repr__

Peter Hansen peter at engcorp.com
Tue Jun 21 14:12:27 CEST 2005

Simon Brunning wrote:
> On 6/15/05, Peter Hansen <peter at engcorp.com> wrote:
>>__repr__ shouldn't be anything, if you don't have an actual need for it.
>>  Neither should __str__.
> Oh, I don't know. __str__ is so frequently useful in debugging and
> logging that I always try and do something useful with it.

Interesting: for the same purpose, I would define __repr__.

But I still define it only when I actually care about the details, since 
otherwise the default __repr__ is always there.  Spending time figuring 
out a potentially more useful __str__/__repr__ (how nice that we've 
confused the issue of which to use, again! ;-) ) is not my idea of a 
good use of time, what with YAGNI and all from XP...


More information about the Python-list mailing list