On Sun, Mar 15, 2020 at 06:27:43PM -0700, Christopher Barker wrote:

> Anyway, Python now has two different ways to "turn this into a string":,
> __str__ and __repr__. I think the OP is suggesting a third, for "pretty
> version". But then maybe folks would want a fourth or fifth, or .....
>
> maybe we should, instead, think about updating the __str__ for standard
> types.
> 
> Is there any guarantee (or even string expectation) that the __str__ for an
> object won't change?

I think there is a strong expectation, even if there's no formal 
guarantee, that the str and repr of builtins and stdlib objects will be 
reasonably stable. If they change, it will break doctests and make a 
huge amount of documentation, blogposts, tutorials, books etc out of 
date.

That's not to say that we can't do it, just that we shouldn't be too 
flippant about it. A few releases back (3.5? 3.4? I forget...) we 
changed the default float repr:

    python2.5 -c "print 2.0/3"
    0.666666666667
    python3.5 -c "print(2.0/3)"
    0.6666666666666666

One advantage of that is that some values are displayed with the nearest 
human-readable exact value, instead the actual value.

One disadvantage of that is that some values are displayed with the 
nearest human-readable exact value, instead of the actual value :-)


> I've always wondered why the standard str(some object) wasn't pretty to
> begin with.

Define "pretty". The main reason I don't use the pprint module at the 
moment is that it formats things like lists into a single long, thin 
column which is *less* attractive than the unformatted list:

    py> pprint.pprint(list(range(200)))
    [0,
     1,
     2,
     3,
     ...
     198,
     199]

I've inserted the ellipsis for brevity, the real output is 200 rows 
tall.

When it comes to floats, depending on what I'm doing, I may consider any 
of these to be "pretty":

* the minimum number of digits which are sufficient to round trip;
* the mathematically exact value, which could take a lot of digits;
* some short number of digits, say, 5, that is "close enough".


-- 
Steven
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/IC6VE64XCU52S3LKYHESOORTWIHETMP3/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to