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

2017-11-22 Thread Ethan Furman
On 11/22/2017 01:40 PM, Ivan Levkivskyi wrote: 22 Лис 2017 22:33 "Ethan Furman" mailto:et...@stoneleaf.us>> пише: Right, the argument is that calling the `list` constructor should return a list -- not a database proxy, not a web page, and not a generator. Then you didn't read my example car

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

2017-11-22 Thread Yury Selivanov
On Wed, Nov 22, 2017 at 6:46 PM, Ivan Levkivskyi wrote: [..] > Just found another example of intuitive behaviour: > async def f(): > ... for i in range(3): > ... yield i > ... async def g(): > ... return [(yield i) async for i in f()] > ... g().send(None) > Traceback

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 21:07, Yury Selivanov wrote: > On Wed, Nov 22, 2017 at 2:37 PM, Ivan Levkivskyi > wrote: > > On 22 November 2017 at 20:33, Guido van Rossum wrote: > >> > >> On Wed, Nov 22, 2017 at 11:12 AM, Ivan Levkivskyi > > >> wrote: > >>> > >>> On 22 November 2017 at 20:05, Guido va

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

2017-11-22 Thread Greg Ewing
Paul Moore wrote: At the moment, I know I tend to treat Python semantics as I always did, but with an implicit proviso, "unless async is involved, when I can't assume any of my intuitions apply". That's *not* a good situation to be in. It's disappointing that PEP 3152 was rejected, because I th

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

2017-11-22 Thread Greg Ewing
Ivan Levkivskyi wrote: while `g = list((yield i) for i in range(3))` is defined as this code: def __gen(): for i in range(3): yield (yield i) g = list(__gen()) Since this is almost certainly not what was intended, I think that 'yield' inside a generator expression shoul

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

2017-11-22 Thread Ivan Levkivskyi
22 Лис 2017 22:33 "Ethan Furman" пише: On 11/22/2017 01:25 PM, Ivan Levkivskyi wrote: > 22 Лис 2017 22:19 "Ethan Furman" пише: > Whether it's inside or outside a function should be irrelevant -- a >> comprehension / generator expression should have >> no influence on the type of the resulting f

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

2017-11-22 Thread Ethan Furman
On 11/22/2017 01:25 PM, Ivan Levkivskyi wrote: 22 Лис 2017 22:19 "Ethan Furman" пише: Whether it's inside or outside a function should be irrelevant -- a comprehension / generator expression should have no influence on the type of the resulting function (and at least synchronous comprehensio

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

2017-11-22 Thread Ivan Levkivskyi
22 Лис 2017 22:19 "Ethan Furman" пише: On 11/22/2017 11:01 AM, Ivan Levkivskyi wrote: I think how to formulate these rules more "precisely" so that they will be > all consistent even if there is a > `yield` inside. > The key idea is that neither comprehensions nor generator expressions > should

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

2017-11-22 Thread Ethan Furman
On 11/22/2017 11:01 AM, Ivan Levkivskyi wrote: I think how to formulate these rules more "precisely" so that they will be all consistent even if there is a `yield` inside. The key idea is that neither comprehensions nor generator expressions should create a function scope surrounding the `expr

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

2017-11-22 Thread Sven R. Kunze
Isn't yield like a return? A return in a list/dict/set comprehension makes no sense to me. So, +1 on SyntaxError from me too. Cheers. On 22.11.2017 21:29, David Mertz wrote: Inasmuch as I get to opine, I'm +1 on SyntaxError. There is no behavior for that spelling that I would find intuitive

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

2017-11-22 Thread David Mertz
Inasmuch as I get to opine, I'm +1 on SyntaxError. There is no behavior for that spelling that I would find intuitive or easy to explain to students. And as far as I can tell, the ONLY time anything has ever been spelled that way is in comments saying "look at this weird edge case behavior in Pytho

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

2017-11-22 Thread Yury Selivanov
On Wed, Nov 22, 2017 at 2:37 PM, Ivan Levkivskyi wrote: > On 22 November 2017 at 20:33, Guido van Rossum wrote: >> >> On Wed, Nov 22, 2017 at 11:12 AM, Ivan Levkivskyi >> wrote: >>> >>> On 22 November 2017 at 20:05, Guido van Rossum wrote: On Wed, Nov 22, 2017 at 10:54 AM, Jelle Zijls

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 20:33, Guido van Rossum wrote: > On Wed, Nov 22, 2017 at 11:12 AM, Ivan Levkivskyi > wrote: > >> On 22 November 2017 at 20:05, Guido van Rossum wrote: >> >>> On Wed, Nov 22, 2017 at 10:54 AM, Jelle Zijlstra < >>> jelle.zijls...@gmail.com> wrote >>> 2017-11-22 9:58 GM

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

2017-11-22 Thread Guido van Rossum
On Wed, Nov 22, 2017 at 11:12 AM, Ivan Levkivskyi wrote: > On 22 November 2017 at 20:05, Guido van Rossum wrote: > >> On Wed, Nov 22, 2017 at 10:54 AM, Jelle Zijlstra < >> jelle.zijls...@gmail.com> wrote >> >>> 2017-11-22 9:58 GMT-08:00 Guido van Rossum : >>> >> (OTOH, await in the same position

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 20:05, Guido van Rossum wrote: > On Wed, Nov 22, 2017 at 10:54 AM, Jelle Zijlstra > wrote > >> 2017-11-22 9:58 GMT-08:00 Guido van Rossum : >> > (OTOH, await in the same position must keep working since it's not broken >> and not unintuitive either.) >> > > This is very q

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 20:15, Guido van Rossum wrote: > On Wed, Nov 22, 2017 at 11:08 AM, Ivan Levkivskyi > wrote: > >> On 22 November 2017 at 19:54, Jelle Zijlstra >> wrote: >> >>> >>> One proposal is to make it so `g` gets assigned a list, and the `yield` >>> happens in the enclosing scope (s

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

2017-11-22 Thread Yury Selivanov
On Wed, Nov 22, 2017 at 2:08 PM, Ivan Levkivskyi wrote: > On 22 November 2017 at 19:54, Jelle Zijlstra > wrote: >> >> >> One proposal is to make it so `g` gets assigned a list, and the `yield` >> happens in the enclosing scope (so the enclosing function would have to be a >> generator). This was

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

2017-11-22 Thread Guido van Rossum
On Wed, Nov 22, 2017 at 11:08 AM, Ivan Levkivskyi wrote: > On 22 November 2017 at 19:54, Jelle Zijlstra > wrote: > >> >> One proposal is to make it so `g` gets assigned a list, and the `yield` >> happens in the enclosing scope (so the enclosing function would have to be >> a generator). This was

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 19:54, Jelle Zijlstra wrote: > > One proposal is to make it so `g` gets assigned a list, and the `yield` > happens in the enclosing scope (so the enclosing function would have to be > a generator). This was the way things worked in Python 2, I believe. > > Another proposal

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

2017-11-22 Thread Guido van Rossum
On Wed, Nov 22, 2017 at 10:54 AM, Jelle Zijlstra wrote: > > > 2017-11-22 9:58 GMT-08:00 Guido van Rossum : > >> Wow, 44 messages in 4 hours. That must be some kind of record. >> >> If/when there's an action item, can someone summarize for me? >> >> The main disagreement seems to be about what thi

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 18:15, Paul Moore wrote: > On 22 November 2017 at 16:47, Ivan Levkivskyi > wrote: > > On 22 November 2017 at 17:43, Paul Moore wrote: > >> > >> On 22 November 2017 at 16:30, Ivan Levkivskyi > >> wrote: > >> > On 22 November 2017 at 17:24, Antoine Pitrou > >> > wrote: >

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

2017-11-22 Thread Jelle Zijlstra
2017-11-22 9:58 GMT-08:00 Guido van Rossum : > Wow, 44 messages in 4 hours. That must be some kind of record. > > If/when there's an action item, can someone summarize for me? > > The main disagreement seems to be about what this code should do: g = [(yield i) for i in range(3)] Currently, t

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

2017-11-22 Thread Serhiy Storchaka
22.11.17 17:10, Ivan Levkivskyi пише: - fix yield in comprehensions I'm working on this. I was going to inline the generating function a long time ago for small performance benefit, but the benefit looked too small for complicating the compiler code. Now I have other reason for doing this.

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

2017-11-22 Thread Guido van Rossum
Wow, 44 messages in 4 hours. That must be some kind of record. If/when there's an action item, can someone summarize for me? -- --Guido van Rossum (python.org/~guido) ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/lis

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

2017-11-22 Thread Ethan Furman
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 i

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 18:15, Paul Moore wrote: > On 22 November 2017 at 16:47, Ivan Levkivskyi > wrote: > > On 22 November 2017 at 17:43, Paul Moore wrote: > >> > >> On 22 November 2017 at 16:30, Ivan Levkivskyi > >> wrote: > >> > On 22 November 2017 at 17:24, Antoine Pitrou > >> > wrote: >

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

2017-11-22 Thread Ethan Furman
On 11/22/2017 09:15 AM, Paul Moore wrote: 1. List comprehensions expand into nested for/if statements in the "obvious" way - with an empty list created to start and append used to add items to it. 1a. Variables introduced in the comprehension don't "leak" (see below). 2. Generator expression

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

2017-11-22 Thread Paul Moore
On 22 November 2017 at 16:47, Ivan Levkivskyi wrote: > On 22 November 2017 at 17:43, Paul Moore wrote: >> >> On 22 November 2017 at 16:30, Ivan Levkivskyi >> wrote: >> > On 22 November 2017 at 17:24, Antoine Pitrou >> > wrote: >> >> Given a comprehension (e.g. list comprehension) is expected to

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 17:50, Paul Moore wrote: > On 22 November 2017 at 16:38, Ivan Levkivskyi > wrote: > > On 22 November 2017 at 17:16, Paul Moore wrote: > >> > >> Docs more importantly than PEP IMO. And are you implying that there's > >> a difference between generator expressions and compre

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

2017-11-22 Thread Yury Selivanov
On Wed, Nov 22, 2017 at 11:11 AM, Paul Moore wrote: [..] > But by the logic you just described, await isn't (or shouldn't be) > allowed, surely? No, that's a stretch. People can understand and actually use 'yield (await foo())', but they usually can't figure what 'yield (yield foo())' actually d

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

2017-11-22 Thread Paul Moore
On 22 November 2017 at 16:38, Ivan Levkivskyi wrote: > On 22 November 2017 at 17:16, Paul Moore 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 expa

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 17:43, Paul Moore wrote: > On 22 November 2017 at 16:30, Ivan Levkivskyi > wrote: > > On 22 November 2017 at 17:24, Antoine Pitrou > wrote: > >> Given a comprehension (e.g. list comprehension) is expected to work > >> nominally as `constructor(generator expression)` > > >

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

2017-11-22 Thread Ivan Levkivskyi
Sorry, forgot some details in the second "definition": try: def __gen(): for i in range(3): yield (yield i) g = list(__gen()) finally: del __gen -- Ivan ___ Python-Dev mailing list Pytho

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

2017-11-22 Thread Paul Moore
On 22 November 2017 at 16:30, Ivan Levkivskyi wrote: > On 22 November 2017 at 17:24, Antoine Pitrou wrote: >> Given a comprehension (e.g. list comprehension) is expected to work >> nominally as `constructor(generator expression)` > > As Yury just explained, these two are not equivalent if there i

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 17:16, Paul Moore wrote: > On 22 November 2017 at 16:08, Ivan Levkivskyi > wrote: > > On 22 November 2017 at 16:56, Yury Selivanov > > wrote: > >> > >> On Wed, Nov 22, 2017 at 10:10 AM, Ivan Levkivskyi > > >> wrote: > >> > On 22 November 2017 at 15:47, Paul Moore wrote:

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 17:24, Antoine Pitrou wrote: > On Wed, 22 Nov 2017 17:08:14 +0100 > Ivan Levkivskyi wrote: > > On 22 November 2017 at 16:56, Yury Selivanov > > wrote: > > > > > On Wed, Nov 22, 2017 at 10:10 AM, Ivan Levkivskyi < > levkivs...@gmail.com> > > > wrote: > > > > On 22 November

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

2017-11-22 Thread Antoine Pitrou
On Wed, 22 Nov 2017 17:08:14 +0100 Ivan Levkivskyi wrote: > On 22 November 2017 at 16:56, Yury Selivanov > wrote: > > > On Wed, Nov 22, 2017 at 10:10 AM, Ivan Levkivskyi > > wrote: > > > On 22 November 2017 at 15:47, Paul Moore wrote: > > [...] > > I'm all for prohibiting using 'yield' exp

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

2017-11-22 Thread Paul Moore
On 22 November 2017 at 15:56, Yury Selivanov wrote: > For synchronous generator expression: > >r = (f(i) for i in range(3)) > > is really: > >def _(): > for i in range(3): > yield f(i) >r = _() > > For an asynchronous generator expression: > >r = (await f(i) for i in

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

2017-11-22 Thread Paul Moore
On 22 November 2017 at 16:08, Ivan Levkivskyi wrote: > On 22 November 2017 at 16:56, Yury Selivanov > wrote: >> >> On Wed, Nov 22, 2017 at 10:10 AM, Ivan Levkivskyi >> wrote: >> > On 22 November 2017 at 15:47, Paul Moore wrote: >> [...] >> I'm all for prohibiting using 'yield' expression in gen

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

2017-11-22 Thread Paul Moore
On 22 November 2017 at 15:56, Yury Selivanov wrote: > "await" in generator expressions works *correct*. I explained it here: > https://bugs.python.org/issue32113 > > To clarify a bit more: > >r = (V for I in X) > > is equivalent to: > >def _(): > for I in X: > yield V >r

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 16:56, Yury Selivanov wrote: > On Wed, Nov 22, 2017 at 10:10 AM, Ivan Levkivskyi > wrote: > > On 22 November 2017 at 15:47, Paul Moore wrote: > [...] > I'm all for prohibiting using 'yield' expression in generator > expressions or comprehensions. The semantics is way to

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 16:53, Paul Moore wrote: > On 22 November 2017 at 15:10, Ivan Levkivskyi > wrote: > > I think there may be a small misunderstanding here. The situation is > > different for comprehensions and generator expressions, > > let me summarize the current state: > [...] > > What d

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

2017-11-22 Thread Yury Selivanov
On Wed, Nov 22, 2017 at 10:10 AM, Ivan Levkivskyi wrote: > On 22 November 2017 at 15:47, Paul Moore wrote: >> >> I generally don't understand "await" in any context, so I deferred >> judgement on that :-) Based on your comment that they are equally >> tricky, I'd suggest we prohibit them both ;-)

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

2017-11-22 Thread Paul Moore
On 22 November 2017 at 15:10, Ivan Levkivskyi wrote: > I think there may be a small misunderstanding here. The situation is > different for comprehensions and generator expressions, > let me summarize the current state: [...] > What do you think? Can you point me to where in the docs it explains

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 15:47, Paul Moore wrote: > I generally don't understand "await" in any context, so I deferred > judgement on that :-) Based on your comment that they are equally > tricky, I'd suggest we prohibit them both ;-) > > Less facetiously, comprehensions are defined in the language

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 15:46, Antoine Pitrou wrote: > On Wed, 22 Nov 2017 15:15:49 +0100 > Ivan Levkivskyi wrote: > > There are many things that I would reject in code review, but they are > > still allowed in Python, > > this is one of the reasons why code reviews exist. Also I am not sure how

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

2017-11-22 Thread Paul Moore
On 22 November 2017 at 14:15, Ivan Levkivskyi wrote: > On 22 November 2017 at 15:09, Paul Moore wrote: >> >> On 22 November 2017 at 13:53, Ivan Levkivskyi >> wrote: >> > On 22 November 2017 at 14:38, Antoine Pitrou >> > wrote: >> >> >> >> On Wed, 22 Nov 2017 15:03:09 +0200 >> >> Serhiy Storchak

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

2017-11-22 Thread Antoine Pitrou
On Wed, 22 Nov 2017 15:15:49 +0100 Ivan Levkivskyi wrote: > There are many things that I would reject in code review, but they are > still allowed in Python, > this is one of the reasons why code reviews exist. Also I am not sure how > `yield` in a comprehension > is more tricky than `await` in a

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 15:09, Paul Moore wrote: > On 22 November 2017 at 13:53, Ivan Levkivskyi > wrote: > > On 22 November 2017 at 14:38, Antoine Pitrou > wrote: > >> > >> On Wed, 22 Nov 2017 15:03:09 +0200 > >> Serhiy Storchaka wrote: > >> > From > >> > > >> > https://stackoverflow.com/ques

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 14:53, Serhiy Storchaka wrote: > 22.11.17 15:25, Ivan Levkivskyi пише: > >> I think this is indeed a problem.. For me the biggest surprise was that >> `yield` inside a comprehension does not turn a surrounding function into >> comprehension, see also >> https://stackoverflo

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

2017-11-22 Thread Paul Moore
On 22 November 2017 at 13:53, Ivan Levkivskyi wrote: > On 22 November 2017 at 14:38, Antoine Pitrou wrote: >> >> On Wed, 22 Nov 2017 15:03:09 +0200 >> Serhiy Storchaka wrote: >> > From >> > >> > https://stackoverflow.com/questions/45190729/differences-between-generator-comprehension-expressions

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

2017-11-22 Thread Ivan Levkivskyi
On 22 November 2017 at 14:38, Antoine Pitrou wrote: > On Wed, 22 Nov 2017 15:03:09 +0200 > Serhiy Storchaka wrote: > > From > > https://stackoverflow.com/questions/45190729/ > differences-between-generator-comprehension-expressions. > > > > g = [(yield i) for i in range(3)] > > > > Syntact

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

2017-11-22 Thread Serhiy Storchaka
22.11.17 15:25, Ivan Levkivskyi пише: I think this is indeed a problem.. For me the biggest surprise was that `yield` inside a comprehension does not turn a surrounding function into comprehension, see also https://stackoverflow.com/questions/29334054/why-am-i-getting-different-results-when-usi

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

2017-11-22 Thread Antoine Pitrou
On Wed, 22 Nov 2017 15:03:09 +0200 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

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

2017-11-22 Thread Ivan Levkivskyi
Serhiy, I think this is indeed a problem. For me the biggest surprise was that `yield` inside a comprehension does not turn a surrounding function into comprehension, see also https://stackoverflow.com/questions/29334054/why-am-i-getting-different-results-when-using-a-list-comprehension-with-corou

<    1   2