[Python-Dev] Python3 compiled listcomp can't see local var
Rob Cliffe
rob.cliffe at btinternet.com
Tue Jun 12 04:38:47 EDT 2018
More information about the Python-Dev mailing list
Tue Jun 12 04:38:47 EDT 2018
- Previous message (by thread): [Python-Dev] Python3 compiled listcomp can't see local var - bug or feature?
- Next message (by thread): [Python-Dev] 2018 Python Language Summit coverage
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Ah yes, I see what you mean: class Test(): x = 1 print (x) # Prints 1 print([x+i for i in range(1,3)]) # NameError (x) Anyway, I apologise for posting to Python-Dev on was a known issue, and turned out to be more me asking for help with development with Python, rather than development of Python. (My original use case was a scripting language that could contain embedded Python code). Thanks to Nick for his original answer. Rob Cliffe On 11/06/2018 23:31, Eric Fahlgren wrote: > On Mon, Jun 11, 2018 at 3:10 PM Rob Cliffe via Python-Dev > <python-dev at python.org <mailto: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. > > <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient> > Virus-free. www.avg.com > <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient> > > > <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20180612/26307041/attachment.html>
- Previous message (by thread): [Python-Dev] Python3 compiled listcomp can't see local var - bug or feature?
- Next message (by thread): [Python-Dev] 2018 Python Language Summit coverage
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-Dev mailing list