__str__ vs __repr__

Donn Cave donn at u.washington.edu
Thu Jun 16 12:35:02 EDT 2005


In article <11b2rnf9his2qd9 at news.supernews.com>,
 "John Roth" <newsgroups at jhrothjr.com> wrote:

> "Donn Cave" <donn at drizzle.com> wrote in message 
> news:1118898421.887013 at yasure...
> > Quoth "John Roth" <newsgroups at jhrothjr.com>:
> > ...
> > | str() should be something that's meaningful to a human being when
> > | it's printed or otherwise rendered.
> >
> > I can't believe how many people cite this explanation - meaningful,
> > friendly, pretty to a human being.  What on earth does this mean,
> > that couldn't be said more unambiguously?
> >
> > According to my impression of common applications for Python programs,
> > rarely would anyone be looking at the output for esthetic gratification.
> > I mean, imagine your users casting an appraising eye over the contours
> > of a phrase emitted by the program, and praising the rhythmic effect of
> > the punctuation it chose to use, or negative space created by tabs.  heh.
> >
> > Whether for human eyes or any destination, properly formed output will
> > carry the information that is required for the application, in a complete
> > and unambiguous way and in the format that is most readily processed,
> > and it will omit extraneous information.  Are we saying anything other
> > than this?
> 
> I thought that's what I said. I fail to see how you derive any other
> meaning from it. Possibly less verbiage and a concerete example
> of how "meaningful" equates to "esthetics that obfustificate understanding"
> and does not equate to "immediately useable, without having to wade
> through a lot of irrelvant mental transformations" would help my 
> understanding.

Not sure if that's a question.  Maybe I can state this more clearly.

Observation:   Most who responded to this question offered an
    explanation of __str__ like "friendly to a human being",
    using one or more of meaningful, friendly or pretty.  The
    online manual uses "informal".

1.  These words are woefully ambiguous, to be applied in computer
    programming.  While there may be some kind of absolute basis
    for esthetic appreciation, that clearly doesn't apply here,
    and terms like this have to be strictly relative to context
    created by the application.
    Summary:  By itself, friendly to a human being is a vacuous
    notion and doesn't help anyone.

2.  If we attempt to support the explanation with a more rigorous
    examination of the principles and how they'd apply to a string
    conversion, it's hard to make this come out favoring str over
    repr.  In the end, they should both be meaningful and friendly,
    by any uncontorted definition of those terms.
    Summary:  To the extent that they have any meaning, they are
    probably applied incorrectly as an explanation of __str__.

So, among other conclusions, the documentation should be changed
to offer a more sensible explanation of __str__.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list