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 <rep...@bugs.python.org>
<http://bugs.python.org/issue18478>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to