PEP 572: Fix and improve examples (thanks Serhiy)
This commit is contained in:
parent
325476b1b7
commit
a502d91476
11
pep-0572.rst
11
pep-0572.rst
|
@ -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)]
|
||||
|
||||
|
|
Loading…
Reference in New Issue