Title: List comprehension in class scope does not have access to class scope
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.5
Status: closed Resolution: duplicate
Dependencies: Superseder: Comprehensions in a class definition mostly cannot access class variable
View: 11796
Assigned To: Nosy List: David Eyk, r.david.murray
Priority: normal Keywords:

Created on 2016-10-11 21:38 by David Eyk, last changed 2016-10-12 00:01 by David Eyk. This issue is now closed.

File name Uploaded Description Edit David Eyk, 2016-10-11 21:38
Messages (3)
msg278505 - (view) Author: David Eyk (David Eyk) Date: 2016-10-11 21:38
I've discovered what appears to be a scoping bug in Python 3.5.1, where the class scope is not available inside a list comprehension defined in the class scope.

Attached is a simple example script, also available at the following gist:

As demonstrated, the script runs fine in Python 2.7.11, but fails in 3.5.1. I can't think of any good reason why the class scope wouldn't be available to the comprehension scope, but if there is a reason, I'm keen to know what it is!
msg278510 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-10-11 23:20
See issue 11796, and the issue it is marked as a duplicate of, for an explanation.
msg278512 - (view) Author: David Eyk (David Eyk) Date: 2016-10-12 00:01
Thanks for the pointer. That seems weird and arbitrary when you think of it in terms of scope, but what can you do?

All the same, thanks for the quick response. :)
Date User Action Args
2016-10-12 00:01:04David Eyksetmessages: + msg278512
2016-10-11 23:20:48r.david.murraysetstatus: open -> closed

superseder: Comprehensions in a class definition mostly cannot access class variable

nosy: + r.david.murray
messages: + msg278510
resolution: duplicate
stage: resolved
2016-10-11 21:38:57David Eykcreate