4 ms·
From: http://www.asmeurer.com/python3-presentation/slides.html#55 http://www.asmeurer.com/python3-presentation/slides.html#55 ... why is this good: def d
by ericfrederich 9y ago
From: http://www.asmeurer.com/python3-presentation/slides.html#55 http://www.asmeurer.com/python3-presentation/slides.html#55
... why is this good:
def dup(n):
for i in range(n):
yield i
yield i
... but this one better?
def dup(n):
for i in range(n):
yield from [i, i]
... it would seem you're needlessly creating (1): another level of generators, and (2) creating a real list
- jessaustin 9y agoYeah at the very least this should be a tuple rather than a list: yield from (i, i) It does use fewer lines, but if that's the point then this would work: yield i; yield i
- maxerickson 9y agoI think for that example it doesn't matter, it is a poor illustration of yield from. In general `yield from` makes delegation inside a generator cleaner.
- kzrdude 9y agoCompare these instead: for i in range(n): yield i yield from range(n)
- falcolas 9y agoBoth seem quite readable, really. Seems similar to the usual "list comprehensions vs. for loop" arguments; to which I always think "use whichever is readable for the given code".
- marcosdumay 9y agoThe thing about "list comprehensions vs. loop" is that you can compose comprehensions, but you must edit loops to change them. I fail to see how anything similar would apply here.
- kevin_thibedeau 9y agoThe old way hit the interpreter on every loop iteration.