John McDonald added the comment: 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. ---------- nosy: +John.McDonald _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue3692> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com