Using __repr__ or __str__ for own printable class?

Steven Taschuk staschuk at telusplanet.net
Mon Apr 28 06:13:52 CEST 2003


Quoth Donn Cave:
  [...problem with an implementation of __repr__ in Mailman...]
> So here's this object.  You can't repr() it, including implicitly
> from the interpreter prompt - you get a traceback instead.  Of course
> that also goes for the list object that contains this object.  To
> me, this is not right, and maybe it ought to be another point to
> consider if we're reconsidering the documentation for __repr__.
> Wish I could specify this better, but I believe there's a minimalist
> way to think about implementing __repr__ that will tend to make it
> less interesting but more reliable.

That's an excellent example of how to do __repr__ wrong, and I
agree that the docs should reflect the underlying principle.  I
think it can be expressed as a consequence of the debugging focus
of __repr__; it's hard to inspect an object while debugging if the
act of inspection itself crashes.

(Related point: a repr should be reliably printable in diverse
environments -- which probably requires sticking to printable
ASCII -- again to ensure debuggability.)

-- 
Steven Taschuk                          staschuk at telusplanet.net
"Its force is immeasurable.  Even Computer cannot determine it."
                           -- _Space: 1999_ episode "Black Sun"





More information about the Python-list mailing list