Rich __repr__

Erik Max Francis max at alcyone.com
Tue Nov 1 19:56:37 EST 2005


Ben Finney wrote:

 > Well that just begs the question: what's a good way (or a Right Way,
 > if that exists) to write a __str__ for a complex class?

Whatever is most useful for the programmer during debugging.

> An idempotent __repr__ output seem to be the ideal, but as you say,
> there are many classes for which it's impossible. What to do in those
> cases? What's the Right Way? What's the common way?

There is no right way.  The common way is to have __str__ print 
something meaningful and useful.  What that is for a particular class 
depends entirely on the circumstances.  There is no uniform solution here.

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   We must all hang together, or, most assuredly, we will all hang
   separately. -- John Hancock



More information about the Python-list mailing list