[Python-Dev] Examples for PEP 572
Terry Reedy
tjreedy at udel.edu
Thu Jul 5 01:02:37 EDT 2018
On 7/4/2018 1:50 PM, Yury Selivanov wrote:
> On Wed, Jul 4, 2018 at 1:35 PM Ivan Pozdeev via Python-Dev
> <python-dev at python.org> wrote:
>>
>> On 04.07.2018 11:54, Serhiy Storchaka wrote:
>
>>>> while total != (total := total + term):
>>>> term *= mx2 / (i*(i+1))
>>>> i += 2
>>>> return total
>>>
>>> This code looks clever that the original while loop with a break in a
>>> middle. I like clever code. But it needs more mental efforts for
>>> understanding it.
>>>
>>> I admit that this is a good example.
>>>
>>> There is a tiny problem with it (and with rewriting a while loop as a
>>> for loop, as I like). Often the body contains not a single break. In
>>> this case the large part of cleverness is disappeared. :-(
>>
>> It took me a few minutes to figure out that this construct actually
>> checks term == 0.
No. Floats are not reals.
The test is that term is small enough *relative to the current total*
that we should stop adding more terms.
>>> 1e50 + 1e30 == 1e50
True
1e30 in not 0 ;-)
> Wow, I gave up on this example before figuring this out (and I also
> stared at it for a good couple of minutes). Now it makes sense. It's
> funny that this super convoluted snippet is shown as a good example
> for PEP 572. Although almost all PEP 572 examples are questionable.
--
Terry Jan Reedy
More information about the Python-Dev
mailing list