Using __repr__ or __str__ for own printable class?
Tim Peters
tim.one at comcast.net
Wed Apr 16 16:22:08 EDT 2003
[Donn Cave]
> But the idea that repr should be the function that conveys that
> "whole truth" is suspiciously consistent with the marshalling idea.
Indeed, the marshal module uses repr() to encode floats in code objects.
The idea behind __repr__ seems prettly clear in the docs:
http://www.python.org/doc/current/ref/customization.html
__repr__(self)
Called by the repr() built-in function and by string conversions
(reverse quotes) to compute the ``official'' string representation of
an object. If at all possible, this should look like a valid Python
expression that could be used to recreate an object with the same
value (given an appropriate environment). If this is not possible, a
string of the form "<...some useful description...>" should be returned.
The return value must be a string object. This is typically used for
debugging, so it is important that the representation is information-
rich and unambiguous.
More information about the Python-list
mailing list