__str__ vs. __repr__

Randall Hopper aa8vb at yahoo.com
Mon Nov 1 18:17:09 CET 1999

1) Is there a convention for what __str__ and __repr__ should return for
2) Or, whatever they return, should they return the same value?
3) If so, why have both in the language?

     Searching the archives yields an example with odd behavior that
suggests they should generally return the same value:

   class c:
       def __str__(self):  return 'foo'
       def __repr__(self): return 'bar'
   >>> str(c())
   >>> str([c(), c(), c()])
   '[bar, bar, bar]'

Randall Hopper
aa8vb at yahoo.com

More information about the Python-list mailing list