http://docs.python.org/3/reference/executionmodel.html#naming-and-binding says "The following are blocks: ... a class definition." and "If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. ..." This is definitely true for functions, but not for classes: foo = 'bar' class C: foo = foo print(C().foo) # bar This is the same for 3.3 and 2.7 with and without '(object)' added. Unless the code is considered to be buggy (probably since forever), the doc should be modified to change 'code block' to 'function code block' or maybe 'module or function code block'. (At near as I can think, the statement is true for modules, but only because globals() == locals(), so that part of the issue does not arise.)
After seeing my suggestion applied, I decided I don't like it. Sorry. A 'function code block' is a 'function suite'. A deeper problem is that the revision now omits the rules for classes. The base problem is that I don't know what the rules are when classes and functions are nested within each other (does anybody, without reading the code?). A puzzling example was posted on python-list yesterday, which I will try to find.
As far as I can see this has been fixed by explaining the special rules for class definitions at the end of the section. Do you think there is anything left to do here, Terry? Perhaps a 2.7 backport of some of the clarifications?
The new language *and* organization introduced sometime during 3.4 is good enough for me. " with an exception that unbound local variables are looked up in the global namespace." covers my example. Thanks for the pointer.
History
Date
User
Action
Args
2022-04-11 14:57:48
admin
set
github: 62678
2019-05-14 18:06:10
xtreak
set
pull_requests: - <pull%5Frequest13235>
2019-05-14 18:04:11
xtreak
set
pull_requests: + <pull%5Frequest13235>
2019-05-14 16:02:26
xtreak
set
pull_requests: - <pull%5Frequest13227>
2019-05-14 16:00:32
xtreak
set
pull_requests: + <pull%5Frequest13227>
2016-05-30 01:24:57
terry.reedy
set
status: open -> closedresolution: out of datemessages: + stage: needs patch -> resolved