for / while else doesn't make sense

Chris Angelico rosuav at gmail.com
Mon May 23 13:33:55 EDT 2016


On Tue, May 24, 2016 at 3:09 AM, Jon Ribbens
<jon+usenet at unequivocal.co.uk> wrote:
> On 2016-05-23, Steven D'Aprano <steve at pearwood.info> wrote:
>> But one thing is certain: very few people, Jon Ribbens being one of them,
>> expects 1/3 to return 0. And that is why Python changed the meaning of
>> the / operator: because using it for integer division was deeply unpopular
>> and a bug magnet.
>
> Making it return floats is also a bug magnet, just for more subtle
> bugs that are harder to diagnose.

See my earlier post. There are four broadly-viable options: int,
float, Fraction, Decimal. *Every one of them* is a bug magnet in one
way or another. Which kind of bug would you like?

* 1235678678/3 == 411892892
* (1/3) + (1/3) + (1/3) != 1
* x != x + 0
* useless reprs after a few rounds of addition

Take your pick. :)

By the way, did you know that Python has support for fraction
literals? Just put this at the top of your code:

import fractions; f = fractions.Fraction(1)

and then you can use the special "tagged literal" syntax, like with
special forms of string literal:

>>> f*22/7 + f*2/11
Fraction(256, 77)

There's even a short-hand form for the common case where the numerator is 1:

>>> f/2 + f/3
Fraction(5, 6)

Very handy, particularly interactively. :)

ChrisA



More information about the Python-list mailing list