Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # 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

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

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

@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

@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

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

muscliary pushed a commit to muscliary/wtfpython that referenced this issue Sep 12, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants