A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2017-November/150709.html below:

[Python-Dev] Tricky way of of creating a generator via a comprehension expression

[Python-Dev] Tricky way of of creating a generator via a comprehension expression [Python-Dev] Tricky way of of creating a generator via a comprehension expressionEthan Furman ethan at stoneleaf.us
Wed Nov 22 12:37:55 EST 2017
On 11/22/2017 05:03 AM, Serhiy Storchaka wrote:

>  From https://stackoverflow.com/questions/45190729/differences-between-generator-comprehension-expressions.
>
>      g = [(yield i) for i in range(3)]
>
> Syntactically this looks like a list comprehension, and g should be a list, right? But actually it is a generator. This
> code is equivalent to the following code:
>
>      def _make_list(it):
>          result = []
>          for i in it:
>              result.append(yield i)
>          return result
>      g = _make_list(iter(range(3)))
>
> Due to "yield" in the expression _make_list() is not a function returning a list, but a generator function returning a
> generator.

The [] syntax says g should be list.  Seems to me we could do either of:

1)  raise if the returned object is not a list;
2)  wrap a returned object in a list if it isn't one already;

In other words, (2) would make

    g = [(yield i) for i in range(3)]

and

    g = [((yield i) for i in range(3))]

be the same.

I have no idea how either of those solutions would interact with async/await.

--
~Ethan~
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4