difference between __repr__() and __str__()
Fredrik Lundh
effbot at telia.com
Mon Feb 28 12:46:58 EST 2000
Gregoire Welraeds wrote:
> __repr__: called to compute the official string representation of an
> object. This should like a valid Python expression that can be used to
> recreate an object with the same value.
>
> __str__: differs from repr in that in does not have to be a valid python
> expression: a more conveniant or concise representation maybe used
> instead.
according to the eff-bot:
__repr__ should something that makes sense to a
programmer, but not necessarily to someone else.
(think "debugging")
(e.g. basic types return a string literal, more complex
types usually return some kind of descriptor, such as
"<foo instance at 912388>" or "<open file 'foo', mode
'r' at 918747>")
__str__ should be used to convert the contents of an
object to a (usually printable) string, whenever that
makes sense (think "class design")
</F>
More information about the Python-list
mailing list