On Mon, Jun 11, 2018 at 3:10 PM Rob Cliffe via Python-Dev < python-dev at python.org> wrote: > Skip, I think you have misunderstood the point I was making. It was > not whether the loop variable should leak out of a list comprehension. > Rather, it was whether a local variable should, so to speak, "leak into" > a list comprehension. And the answer is: it depends on whether the code > is executed normally, or via exec/eval. Example: > > def Test(): > x = 1 > print([x+i for i in range(1,3)]) # Prints [2,3] > exec('print([x+i for i in range(1,3)])') # Raises NameError (x) > Test() > > I (at least at first) found the difference in behaviour surprising. > Change 'def' to 'class' and run it again. You'll be even more surprised. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20180611/0934b1d2/attachment.html>
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4