[Tim]
In a different thread I noted that I sometimes want to write code like this: ... while any(n % (thisp := p) == 0 for p in small_primes): n //= thisp ...
[Ryan Gonzalez <rymg19@gmail.com>]
Couldn't you just do:
def first(it): return next(it, None)
while (item := first(p for p in small_primes if n % p == 0)): # ...
In the "different thread" I mentioned above, I already noted that kind of spelling. I'm not at a loss to think of many ways to spell it ;-) The point of this thread was intended to be about the semantics of binding expressions in comprehensions. For that purpose, the PEP noting that total = 0 progressive_sums = [total := total + value for value in data] fails too is equally relevant. Of course there are many possible ways to rewrite that too that would work. That doesn't change that the failing attempts "look like they should work", but don't, but could if the semantics of ":=" were defined differently inside magically-created anonymous lexically nested functions