PEP 572: Fix and improve examples (thanks Serhiy)

This commit is contained in:
Chris Angelico 2018-03-01 02:20:49 +11:00
parent 325476b1b7
commit a502d91476
1 changed files with 9 additions and 2 deletions

View File

@ -64,8 +64,8 @@ These list comprehensions are all approximately equivalent::
# Extra 'for' loop - see also Serhiy's optimization
stuff = [[y, x/y] for x in range(5) for y in [f(x)]]
# Iterating over a genexp (similar to the above but reordered)
stuff = [[y, x/y] for y in (f(x) for x in range(5))]
# Iterating over a genexp
stuff = [[y, x/y] for x, y in ((x, f(x)) for x in range(5))]
# Expanding the comprehension into a loop
stuff = []
@ -73,6 +73,13 @@ These list comprehensions are all approximately equivalent::
y = f(x)
stuff.append([y, x/y])
# Wrapping the loop in a generator function
def g():
for x in range(5):
y = f(x)
yield [y, y]
stuff = list(g)
# Using a statement-local name
stuff = [[(f(x) as y), x/y] for x in range(5)]