[issue2237] Inconsistent variable lookup

Amaury Forgeot d'Arc report at bugs.python.org
Tue Mar 4 23:21:50 CET 2008


Amaury Forgeot d'Arc added the comment:

> ... adding a line to end the code AFFECTS its beginning ...

Exactly. Please see
http://docs.python.org/dev/reference/executionmodel.html
"""
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 rule is subtle. Python lacks declarations
and allows name binding operations to occur anywhere within a code
block. The local variables of a code block can be determined by scanning
the entire text of the block for name binding operations.
"""

For more general discussions, see also:
http://www.python.org/doc/faq/programming/#what-are-the-rules-for-local-and-global-variables-in-python
http://docs.python.org/dev/tutorial/classes.html#python-scopes-and-name-spaces

In other words, this is one of the key features of the python language,
and I don't see how it could change.

----------
nosy: +amaury.forgeotdarc
resolution:  -> invalid
status: open -> closed

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2237>
__________________________________


More information about the Python-bugs-list mailing list