Using __repr__ or __str__ for own printable class?

Erik Max Francis max at alcyone.com
Tue Apr 22 23:10:39 CEST 2003


Ted Drain wrote:

> I think I'd rather see the behavior change when you do:
> >>> s
> To call __str__ instead of __repr__.  It seems to me that repr is a
> specialized functionality that should be explicitly asked for when
> needed.  __str__ is the more normal output and should be the default
> in 'print s' or 's'.

I disagree.  In the interactive interpreter, I want to see the repr of
evaluations of the expressions I type in, not the str.  If I see

>>> someInvolvedExpression
2

Is that the numeric 2? the string '2'? the string '2 \t '? some custom
type whose __str__ is defined to return 2?

When I'm using the interactive interpreter, I'm essentially in a
"debugging" mentality, whether or not I'm actually debugging.  I want to
see the most unambiguous and detailed information about what I'm
evaluating.  After all, if I wanted the __str__, I'd just write

>>> str(someInvolvedExpression)

or

>>> print someInvolvedExpression

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ There are defeats more triumphant than victories.
\__/ Montaigne
    Discord / http://www.alcyone.com/pyos/discord/
 Convert dates from Gregorian to Discordian.




More information about the Python-list mailing list