This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author John.McDonald
Recipients John.McDonald, ajaksu2, georg.brandl, kaizhu
Date 2013-07-05.20:58:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1373057911.79.0.783796839199.issue3692@psf.upfronthosting.co.za>
In-reply-to
Content
Could we possibly revisit this? This feels like an implementation detail, not something specified. Consider the different cases:

Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...   b = 5
...   print(locals())
...   x = [i*i for i in range(b)]
...
{'__qualname__': 'A', '__locals__': {...}, '__module__': '__main__', 'b': 5}

This case works, and shows that 'b' is clearly in locals(). 

>>> class A:
...   b = 5
...   c = b * b
...   print(locals())
...   d = []
...   for i in range(b):
...     d.append(i*i)
...   print(locals())
...   e = [i*i for i in range(b) if i*i < c]
...
{'__locals__': {...}, '__qualname__': 'A', 'b': 5, '__module__': '__main__', 'c': 25}
{'__qualname__': 'A', 'b': 5, '__module__': '__main__', 'c': 25, 'i': 4, 'd': [0, 1, 4, 9, 16], '__locals__': {...}}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 9, in A
  File "<stdin>", line 9, in <listcomp>
NameError: global name 'c' is not defined

Again, it feels really arbitrary that the variable can be used in certain places in the list comprehension, but not others.

And of course, all of this works properly if you place the definitions either at global scope or within a function.
History
Date User Action Args
2013-07-05 20:58:31John.McDonaldsetrecipients: + John.McDonald, georg.brandl, ajaksu2, kaizhu
2013-07-05 20:58:31John.McDonaldsetmessageid: <1373057911.79.0.783796839199.issue3692@psf.upfronthosting.co.za>
2013-07-05 20:58:31John.McDonaldlinkissue3692 messages
2013-07-05 20:58:31John.McDonaldcreate