[Python-Dev] Tricky way of of creating a generator via a comprehension expression
Antoine Pitrou
solipsis at pitrou.net
Thu Nov 23 08:04:22 EST 2017
More information about the Python-Dev mailing list
Thu Nov 23 08:04:22 EST 2017
- Previous message (by thread): [Python-Dev] Tricky way of of creating a generator via a comprehension expression
- Next message (by thread): [Python-Dev] Tricky way of of creating a generator via a comprehension expression
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Thu, 23 Nov 2017 14:54:27 +0200 Serhiy Storchaka <storchaka at gmail.com> wrote: > 23.11.17 14:30, Antoine Pitrou пише: > > On Thu, 23 Nov 2017 14:17:32 +0200 > > Serhiy Storchaka <storchaka at gmail.com> wrote: > >> > >> I used the "yield" statement, but I never used the "yield" expressions. > >> And I can't found examples. Could you please present a real-world use > >> case for the "yield" (not "yield from") expression? > > > > Of course I can. "yield" expressions are important for writing > > Python 2-compatible asynchronous code while avoiding callback hell: > > > > See e.g. http://www.tornadoweb.org/en/stable/gen.html > > or https://jdb.github.io/concurrent/smartpython.html > > > > There are tons of real-world code written using this scheme (as opposed > > to almost no real-world code, even Python 2-only, using "yield" in > > comprehensions or generation expressions). > > Thank you. The tornado examples contain the following equivalence code > for `results = yield multi(list_of_futures)`: > > results = [] > for future in list_of_futures: > results.append(yield future) > > Couldn't this by written as `results = [(yield future) for future in > list_of_futures]`? See my answer to Ivan above. The code isn't actually equivalent :-) But, yes, this construct *could* be useful if you wanted to schedule futures serially (as opposed to in parallel). However, since it doesn't work on Python 3.x, and the main reason to use "yield" coroutines (even with Tornado) instead of "async/await" is for compatibility, solving the "yield in a comprehension" problem in 3.7 wouldn't make things any better IMO. Regards Antoine.
- Previous message (by thread): [Python-Dev] Tricky way of of creating a generator via a comprehension expression
- Next message (by thread): [Python-Dev] Tricky way of of creating a generator via a comprehension expression
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-Dev mailing list