Repr or Str ?

Donn Cave donn at u.washington.edu
Tue Feb 6 14:00:39 EST 2007


In article <1170758827.396931.80680 at m58g2000cwm.googlegroups.com>,
 "Johny" <python at hope.cz> wrote:

> Where and when is good/nescessary to use `repr`  instead of `str` ?
> Can you please explain the differences

You expect repr to include information that you might call
`meta-data' or `type' -- object class and so forth.  To the
extent that this is of any interest, it's more or less equally
of interest with all objects.

If you go to the trouble to support str separately, it's a data
conversion and of course should render only the data.  An application
should be able to use str() to force data to string type (that's
what I mean by conversion.)  If the object can't sensibly be
converted to string type, then normally __str__ is omitted, and
defaults to __repr__.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list