-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
PEP 798: Adjust Generator Expression Semantics #4547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: Jelle Zijlstra <[email protected]>
Alternative Generator Expression Semantics | ||
------------------------------------------ | ||
|
||
Another point of discussion centered around the semantics of unpacking in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth showing some examples of what the actual difference in semantics is between yield from
and the current approach with for x in ...: yield x
. I think it matters if you .send
something into the genexp?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, my understanding is that using yield from
would mean that, if the thing we're unpacking is itself a generator, then anything we send to our top-level generator would actually go to the one we're unpacking. With an explicit loop, that doesn't happen. My gut feeling is that wanting to delegate to the thing being unpacked is probably a pretty niche situation (I'm not sure I know of a good practical use case for it), though I could certainly be wrong there.
Either way, I agree it's worth making that difference more explicit. I'll add a paragraph here, but it might not be until tomorrow. Let's hold off on merging until then.
Just a heads-up that I probably won't have my updates in tonight, since I'm still going back and forth in my head about what the right decision is here... In case it's of interest, here's what I'm currently thinking:
So I think I might actually be slowly convincing myself that the option you voted for in the poll might be the right choice 😄. That way we could use But, tl;dr: I'm going to need to think about this a bit more (and probably follow up in the Discourse thread, too). |
There's no hurry! The only sort-of-deadline is the Python 3.15 feature freeze next May, and we have plenty of time before that. Here's some thoughts from me, but I don't have firm views either.
With PEP 798 unpacking and >>> def make_gen(i):
... val = yield "input:"
... yield val * i
...
>>> g = (*make_gen(i) for i in range(5, 10))
>>>
>>> g.send(None)
'input:'
>>> g.send(1)
5
>>> g.send(None)
'input:'
>>> g.send(3)
18
>>> g.send(None)
'input:'
>>> g.send(4)
28 Well, not that practically useful, but maybe you'd be able to come up with a way to use this that is actually good for something useful. This provides an argument that it's reasonable to go with |
Thanks for the input! I agree on all fronts. And actually, the need to repeatedly send |
I think you don't actually need to send Nones for every one of them, e.g. this worked: >>> g.send(4)
28
>>> g.send(42)
'input:'
>>> g.send(43)
344 But it's definitely a complicated interface to work with. |
Ah, sorry, yes, but every so often one thing that you send will effectively be ignored, right? That is, the output above would have been the same with any other object in place of |
True, though I could have gotten it out if I had made the inner generator do something with the result of the second yield. |
Oh, interesting. Yeah, you're right. Sorry; I was misunderstanding what was actually happening there! |
The main change here is adjusting the proposed semantics of unpacking in generator expressions and adding a section briefly outlining the pros and cons of the alternatives, but I also:
📚 Documentation preview 📚: https://pep-previews--4547.org.readthedocs.build/