__str__ vs. __repr__

Tino Wildenhain tino at wildenhain.de
Mon Nov 1 15:50:22 EST 1999


Hallo Randell,

Randall Hopper wrote:
> 
> 1) Is there a convention for what __str__ and __repr__ should return for
>    classes?
> 2) Or, whatever they return, should they return the same value?
> 3) If so, why have both in the language?
> 
>      Searching the archives yields an example with odd behavior that
> suggests they should generally return the same value:

If you test, its oviously (I thin its mentioned somewhere in the docs
too) :

__str__ represents an human-readable version of the object as string for
use
with "print" or formatet string ("%s")
__repr__ should produce a form that can be cut & pasted to construct the
named object.

print "Hello"
Hello

"Hello"
'Hello'

see the difference?
 
best regards

Tino Wildenhain




More information about the Python-list mailing list