[issue18478] Class bodies: when does a name become local?

New submission from Terry J. Reedy: 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.) ---------- assignee: docs@python components: Documentation messages: 193205 nosy: docs@python, terry.reedy priority: normal severity: normal stage: needs patch status: open title: Class bodies: when does a name become local? type: behavior versions: Python 2.7, Python 3.3, Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue18478> _______________________________________

Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- nosy: +ezio.melotti _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue18478> _______________________________________

Nitika Agarwal added the comment: Hey. I am working on this patch. ---------- nosy: +nitika _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue18478> _______________________________________

Nitika Agarwal added the comment: Hi, Please review my patch attached. ---------- keywords: +patch Added file: http://bugs.python.org/file34441/issue18478.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue18478> _______________________________________

Nitika Agarwal added the comment: Hi, I have attached the patch for the issue.Please someone review my patch. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue18478> _______________________________________

Terry J. Reedy added the comment: 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. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue18478> _______________________________________

R. David Murray added the comment: 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? ---------- nosy: +r.david.murray _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue18478> _______________________________________

Terry J. Reedy added the comment: 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. ---------- resolution: -> out of date stage: needs patch -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue18478> _______________________________________

Change by Karthikeyan Singaravelan <tir.karthi@gmail.com>: ---------- pull_requests: +13227 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue18478> _______________________________________

Change by Karthikeyan Singaravelan <tir.karthi@gmail.com>: ---------- pull_requests: -13227 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue18478> _______________________________________

Change by Karthikeyan Singaravelan <tir.karthi@gmail.com>: ---------- pull_requests: +13235 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue18478> _______________________________________

Change by Karthikeyan Singaravelan <tir.karthi@gmail.com>: ---------- pull_requests: -13235 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue18478> _______________________________________
participants (5)
-
Ezio Melotti
-
Karthikeyan Singaravelan
-
Nitika Agarwal
-
R. David Murray
-
Terry J. Reedy