[Python-ideas] A comprehension scope issue in PEP 572

Brendan Barnwell brenbarn at brenbarn.net
Mon May 7 14:33:43 EDT 2018


On 2018-05-06 18:32, Tim Peters wrote:
> In a different thread I noted that I sometimes want to write code like this:
>
>      while any(n % p == 0 for p in small_primes):
>          # divide p out - but what's p?
>
> But generator expressions hide the value of `p` that succeeded, so I
> can't.  `any()` and `all()` can't address this themselves - they
> merely work with an iterable of objects to evaluate for truthiness,
> and know nothing about how they're computed.  If you want to identify
> a witness (for `any()` succeeding) or a counterexample (for `all()`
> failing), you need to write a workalike loop by hand.

	I agree that is a limitation, and I see from a later message in the 
thread that Guido finds it compelling, but personally I don't find that 
that particular case such a showstopper that it would tip the scales for 
me either way.  If you have to write the workalike look that iterates 
and returns the missing value, so be it.  That's not a big deal.

-- 
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is no 
path, and leave a trail."
    --author unknown


More information about the Python-ideas mailing list