On Sat, Apr 21, 2018 at 6:38 PM, Anthony Flury via Python-Dev <python-dev at python.org> wrote: > On 21/04/18 08:46, Chris Angelico wrote: >> >> doubled_items = [x for x in (items := get_items()) if x * 2 in items] >> >> This will leak 'items' into the surrounding scope (but not 'x'). > > At the risk of stating the obvious - wasn't there work in Python 3 to > prevent leakage from comprehensions ? >> >> [x for x in x if x] # This works >> [x for y in x if x := y] # UnboundLocalError > > > The standard library example given earlier notwithstanding, I can see no > benefit in using the same name as the iterator and the loop target name. To > be honest I have trouble parsing that first version, and keeping track of > which x is which (especially which x is being used in the conditional > clause) : surely this would be better : [x_item for x_item in x if x_item] > > Your 2nd example makes no sense to me as to the intention of the code - the > re-use of the name x is confusing at best. > I agree. The change in behaviour caused by PEP 572 is basically only going to be visible if you reuse a name, or in a very few other cases like yield expressions: def gen(): yield [x for x in (yield 1)] g = gen() next(g) g.send(range(5)) Once again, the outermost iterable is bizarre in this way. ChrisA
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4