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

Ivan Levkivskyi levkivskyi at gmail.com
Wed Nov 22 11:58:25 EST 2017


On 22 November 2017 at 17:50, Paul Moore <p.f.moore at gmail.com> wrote:

> On 22 November 2017 at 16:38, Ivan Levkivskyi <levkivskyi at gmail.com>
> wrote:
> > On 22 November 2017 at 17:16, Paul Moore <p.f.moore at gmail.com> wrote:
> >>
> >> Docs more importantly than PEP IMO. And are you implying that there's
> >> a difference between generator expressions and comprehensions? I
> >> thought both were intended to behave as if expanded to a function
> >> containing nested for loops? Nothing said in this thread so far (about
> >> semantics, as opposed to about current behaviour) implies there's a
> >> deliberate difference.
> >
> >
> > I think there may be a difference:
> >
> > comprehension `g = [(yield i) for i in range(3)]` is defined as this
> code:
> >
> >     __result = []
> >     __i = None
> >     try:
> >         for __i in range(3):
> >             __result.append(yield __i)
> >         g = __result
> >     finally:
> >         del __result, __i
>
> Not in the docs, it isn't...


Yes, since there is almost nothing there, this is what I _propose_ (or
actually Serhiy proposed it first)


> The docs explicitly state that a new
> scope is involved.
>

But docs don't say it is a _function_ scope. The meaning of that statement
(as I understand it) is just that the loop variable doen't leak from
comprehension.

--
Ivan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171122/8580a38f/attachment.html>


More information about the Python-Dev mailing list