Emanuel Barry added the comment: Using a simple metaclass shows that definition order is not at fault here:
>>> class PrintOrder(dict): ... def __setitem__(self, item, value): ... print(item, value) ... super().__setitem__(item, value) ... >>> class Show(type): ... def __prepare__(name, bases): return PrintOrder() ... >>> class A(metaclass=Show): ... B = range(10) ... C = frozenset([4, 5, 6]) ... D = list(i for i in B) ... E = list(i for i in B if i in C) ... __module__ __main__ __qualname__ A B range(0, 10) C frozenset({4, 5, 6}) D [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 5, in A File "<stdin>", line 5, in <genexpr> NameError: name 'C' is not defined However, the following works: >>> def local_definition(): ... F = frozenset([4, 5, 6]) ... class A(metaclass=Show): ... B = range(10) ... C = frozenset([4, 5, 6]) ... D = list(i for i in B) ... E = list(i for i in B if i in F) ... return A ... >>> local_definition() __module__ __main__ __qualname__ local_definition.<locals>.A B range(0, 10) C frozenset({4, 5, 6}) D [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] E [4, 5, 6] <class '__main__.local_definition.<locals>.A'> Sounds like either an inconsistency between 'for' and 'if' parts of generator expressions, or something weird about the way generator expressions work that I'm unaware of. Furthermore, this isn't documented in the tutorial, the functional programming How-To, the 2.4 release notes or the PEP 289: https://docs.python.org/3/tutorial/classes.html#generator-expressions https://docs.python.org/3/howto/functional.html#generator-expressions-and-list-comprehensions https://docs.python.org/3/whatsnew/2.4.html#pep-289-generator-expressions https://www.python.org/dev/peps/pep-0289/ ---------- nosy: +ebarry stage: -> needs patch _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue26951> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com