what's the use of __repr__?when shall I use it?

Greg Ewing greg at cosc.canterbury.ac.nz
Sun Apr 3 23:52:19 EDT 2005


Vikram wrote:
> __repr__ should return something that when eval'ed yields an identical
> object (if possible).

That's strictly possible in so few cases that it's not
really a very helpful guideline, in my opinion.

I find the following view more helpful:

* str() is for producing the normal output of a program,
   to be seen by the user.

* repr() is for debugging output, and should indicate
   reasonably unambiguously the *type* of the object.
   When debugging, it's often at least as important to
   know what type of object you have as what value it
   has.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list