New submission from Armin Rigo:

This is a repeat of the old issue 532860: "NameError assigning to class in a 
func".  It is about class statements' variable lookups, which has different 
behavior at module level or in a nested scope:

def f(n):
    class A:
        n = n     # doesn't work, tries to look up 'n' as a global

The point of repeating this very old issue is the much more recent issue 17853: 
"Conflict between lexical scoping and name injection in __prepare__".  This was 
a slightly different problem, but resolved by adding the exact opcode that 
would be needed to fix the old issue too: LOAD_CLASSDEREF.  It's an opcode 
which looks in the locals dict for a name, and if not found, falls back to 
freevars instead of to globals.

The present bug report is here to argue that this new opcode should be used a 
bit more systematically by the compiler.  Contrary to the conclusions reached 
in the very old bug report, I argue that nowadays it would seem reasonable to 
expect that the previous example should work.  By no means is it an essential 
issue in my opinion, but this would probably be a slight simplification and 
prevent corner-case surprizes.  Moreover it probably leads to a simplification 
in the compiler: no need to track which variables are local or not in class 
bodies --- instead just compile the loading of 'n' above as a LOAD_CLASSDEREF 
without worrying about the fact that the same 'n' is also assigned to in the 
same scope.

----------
components: Interpreter Core
messages: 206149
nosy: arigo
priority: normal
severity: normal
status: open
title: Missing nested scope vars in class scope (bis)

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue19979>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to