28.02.18 16:06, Chris Angelico пише:
On Thu, Mar 1, 2018 at 12:49 AM, Serhiy Storchaka <storch...@gmail.com> wrote:
Other options:
g = (f(x) for x in range(5))
stuff = [[y, y] for y in g]
That's the same as the one-liner, but with the genexp broken out. Not
sure it helps much as examples go?
It is more readable. But can't be used as an expression.
def g():
for x in range(5):
y = f(x)
yield [y, y]
stuff = list(g)
You're not the first to mention this, but I thought it basically
equivalent to the "expand into a loop" form. Is it really beneficial
to expand it, not just into a loop, but into a generator function that
contains a loop?
It is slightly faster (if the list is not too small). It doesn't leak a
temporary variable after loop. And in many cases you don't need a list,
an iterator would work as well. In these cases it is easy to just drop
calling list().
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/