On Fri, Mar 7, 2014 at 9:39 PM, Steven D'Aprano email@example.com wrote:
On Fri, Mar 07, 2014 at 11:38:34AM +1100, Chris Angelico wrote:
Hmm. Is the rounding done by float.__str__() an attractive nuisance?
It's not rounding precisely. Starting in Python 3.1, the float __repr__ will display the shortest decimal string that doesn't change the float's value.
Reading the issue tracker history for this change is informative:
To the guys who worked on that, I take my hat off to you all.
That effect, yes. Let's call it "the magic of float.__str__", because it really is pretty amazing.
But it's still post-processing magic. It means that strings appear to round-trip through floats, as long as you're a long way within the available precision; but as soon as you do operations, that ceases to be the case. I think it's great for display, but is putting that into __repr__ (at least, they do appear to be the same) an attractive nuisance, in that it encourages people to treat float("...") as a true representation?
Don't get me wrong, it's a really awesome feature. It just happens to have been partially responsible for this thread, which I think is approaching 300 posts.