Using __repr__ or __str__ for own printable class?
Steven Taschuk
staschuk at telusplanet.net
Wed Apr 16 22:37:38 EDT 2003
Quoth Peter Hansen:
[...]
> I've always considered __repr__ to be for use in *debugging* the
> application, where a *debugging* log needs to display information,
> or in a traceback. Since it's next to impossible for most interesting
> objects to be reproduced easily from the output of __repr__ if it's
> also desirable that the output be meaningful and useful to a human
> reader, I don't buy into the 'eval(repr(x)) == x' theory very much,
> other than to note that it's the most obvious thing to do for
> primitive data types.
There are, I admit, lots of objects for which the eval(repr(x)) ==
x theory doesn't make sense. (File objects, for example.) But in
my (admittedly not very broad) experience, these tend to be
objects for which == doesn't make sense in the first place.
Imho the notion that eval(repr(x)) == x theory makes lots of sense
for value objects generally. (See, e.g., the timbot's example of
datetime.date.)
> I've never had any confusion in my own mind about when it's
> appropriate to use one or the other. Perhaps this perspective
> should be used in the documentation, rather than the focus on
> reproducing objects from their __repr__.
*shrug* As quoted in another post, the docs explicitly state that
repr "is typically used for debugging". I suppose it could be
more emphasized.
--
Steven Taschuk w_w
staschuk at telusplanet.net ,-= U
1 1
More information about the Python-list
mailing list