Beyond __str__ and __repr__? (Was Re: Python doesn't know how to do math?)

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Mon Apr 29 18:45:30 EDT 2002


Tim Peters <tim.one at comcast.net> wrote:
>Except in the details, this is a lot like ssctsoos ("str() special-casing
>the snot out of strings").  That's a hypothetical function discussed at
>nauseating length several times over the years, at least on Python-Dev, but
>not recently.

[ snip background details about 'ssctsoos' (what a strange name!)]

>In other words, ssctsoos(x) *was* str(x), except that container types
>"passed it down" instead of passing repr down, and *something* special
>needed to be done for strings.

[snip]

>IOW, str() and repr() are both fine for their intended purposes, but the
>interactive prompt specifically wants neither as defined today.  What most
>people want there is str(), with containers passing str down to containees
>instead of repr.  Alas, the niggling 2 vs "2" problem has killed every
>attempt to date.

So we have identified at least the following desirable properties:

- passing down structures (lists, dicts, tuples).  Call it self-recursive.
- differentiating between 2 and "2".  Call it diff-num-str.
- showing floating numbers without extra precision.  Call it simple-float.
- showing national characters as in str().  Call it smart-char.

It appears that simply modifying str() or repr() or the interactive prompt
cannot achieve all these without losing some other nice properties they have
today. 

If that observation is correct, then introducing disp() __disp__() to take
care of all these issues in one stroke would appear to be worth it.  (Of
course the name can be chosen later.)

Huaiyu



More information about the Python-list mailing list