You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Global scoping when shadowing local names in class definitions #79142
weird_class_scope.py: A broader example, demonstrating it only works with classes, and the scope the variable comes from is global
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2018-10-12.20:56:52.152>created_at=<Date2018-10-11.19:15:04.232>labels= ['interpreter-core', '3.8', 'type-bug', '3.7', 'invalid']
title='Global scoping when shadowing local names in class definitions'updated_at=<Date2018-10-12.20:56:52.142>user='https://github.com/multun'
It seems that when there is an assignation in the class body, prior usages of the variables in the class body are done by loading the variable using LOAD_NAME instead of LOAD_CLASSDEREF:
Disassembly of <code object wtf at 0x7fa1b8b01390, file "lolmul.py", line 7>:
[...]
Questions about whether basic syntax behavior is correct should be asked elsewhere, such as python-list. A proper answer requires a careful reading of the doc and a non-trivial answer. Someone on python-list should do that.
Classes and scoping are known to have some non-intuitive corner cases. Quite aside from the fact that the default assumption should be that the implementation of heavily used syntax is correct, my memory is that what you see is correct. But I have not read the appropriate parts of the doc for a few years.
PS. 3.4 and 3.5 currently only get security fixes. The same will be true of 3.6 sometime next year.
multun commented on Oct 12, 2018
multunmannequin
MannequinAuthor
More actions
Closing, this is documented, and as such most likely intended.
Activity
multun commentedon Oct 11, 2018
Hello,
Is this behavior legit ?
Best regards,
--
Victor "multun" Collod
seirl commentedon Oct 12, 2018
Tracking down the issue a bit further:
prints:
It seems that when there is an assignation in the class body, prior usages of the variables in the class body are done by loading the variable using LOAD_NAME instead of LOAD_CLASSDEREF:
Disassembly of <code object wtf at 0x7fa1b8b01390, file "lolmul.py", line 7>:
[...]
8 8 LOAD_NAME 3 (print)
10 LOAD_CLASSDEREF 0 (a)
12 CALL_FUNCTION 1
14 POP_TOP
16 LOAD_CONST 1 (None)
18 RETURN_VALUE
Disassembly of <code object wtf2 at 0x7fa1b8b018a0, file "lolmul.py", line 10>:
[...]
11 8 LOAD_NAME 3 (print)
10 LOAD_NAME 4 (a)
12 CALL_FUNCTION 1
14 POP_TOP
12 16 LOAD_CONST 1 (2)
18 STORE_NAME 4 (a)
20 LOAD_CONST 2 (None)
22 RETURN_VALUE
terryjreedy commentedon Oct 12, 2018
Questions about whether basic syntax behavior is correct should be asked elsewhere, such as python-list. A proper answer requires a careful reading of the doc and a non-trivial answer. Someone on python-list should do that.
Classes and scoping are known to have some non-intuitive corner cases. Quite aside from the fact that the default assumption should be that the implementation of heavily used syntax is correct, my memory is that what you see is correct. But I have not read the appropriate parts of the doc for a few years.
PS. 3.4 and 3.5 currently only get security fixes. The same will be true of 3.6 sometime next year.
multun commentedon Oct 12, 2018
Closing, this is documented, and as such most likely intended.
"A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace."
https://docs.python.org/3.7/reference/executionmodel.html#builtins-and-restricted-execution