Skip to content
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

Add section explaining 'yield' in list comp/generator exp #32

Closed
chris-rands opened this issue Sep 6, 2017 · 4 comments
Closed

Add section explaining 'yield' in list comp/generator exp #32

chris-rands opened this issue Sep 6, 2017 · 4 comments

Comments

@chris-rands
Copy link

@chris-rands chris-rands commented Sep 6, 2017

These examples illustrate what for me is one of the most confusing behaviours in the whole of Python:

>>> [(yield x) for x in ('a','b')]
<generator object <listcomp> at 0x7f69d5379ba0>
>>> list([(yield x) for x in ('a','b')])
['a', 'b']
>>> list((yield x) for x in ('a','b'))
['a', None, 'b', None]

A relevant SO question and bug report

@satwikkansal
Copy link
Owner

@satwikkansal satwikkansal commented Oct 11, 2017

Ah, I went through the SO thread and the bug report but unfortunately couldn't come up with a simple and short explanation. So I think for the time being, it's better to link to the resources you shared. Meanwhile I'll try to come up with a simpler explanation with the help of examples.

@satwikkansal
Copy link
Owner

@satwikkansal satwikkansal commented Oct 11, 2017

@chris-rands Thanks for your suggestion. I've added this snippet in #45, and will try my best to add a detailed explanation for the same.

@chris-rands
Copy link
Author

@chris-rands chris-rands commented Nov 21, 2017

@satwikkansal Thank you for incorporating my suggestion and apologies for not responding before; I don't think I can provide a better explanation than that given in the links but it might be worth adding that this behaviour described may change because this really is a bug and not a feature. As the bug report states, for PyPy these constructs now raise a SyntaxWarning (but not for CPython).

@chris-rands
Copy link
Author

@chris-rands chris-rands commented Feb 6, 2018

Update: see the more recent discussion on the bug report bugs.python.org/issue10544; this syntax will sometimes raise a DeprecationWarning in Py 3.7 and then a SyntaxError from Py 3.8 docs.python.org/3.7/whatsnew/3.7.html#deprecated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.