[Python-Dev] Examples for PEP 572
Steven D'Aprano
steve at pearwood.info
Wed Jul 4 14:21:46 EDT 2018
On Wed, Jul 04, 2018 at 08:33:17PM +0300, Ivan Pozdeev via Python-Dev wrote:
> >>while total != (total := total + term):
> >> term *= mx2 / (i*(i+1))
> >> i += 2
> >>return total
[...]
> It took me a few minutes to figure out that this construct actually
> checks term == 0.
That's badly wrong.
Hint: it's floating point code. total and term are floats, so things
like this are possible:
py> total = 9.5e95
py> term = 1.2
py> total + term == total
True
As you can see, term is nothing even close to zero.
> So, this example abuses the construct to do something it's not designed
> to do: perform an unrelated operation before checking the condition.
Well, that's one opinion.
> (Cue attempts to squeeze ever mode code here.) I would fail it in review.
Okay.
--
Steve
More information about the Python-Dev
mailing list