On Tue, Jul 16, 2013 at 9:25 AM, Jack Bates <tdh...@nottheoilrig.com> wrote:
> Ah, thank you Chris Angelico for explaining how this is like what happens
> with default arguments to a function and Joshua Landau for pointing out how
> assignments inside class bodies refer to properties of "self" on the LHS. It
> makes sense now. Only I'm struggling to find where the behavior is defined
> in the language reference. Can someone please help point me to where in the
> language reference this is discussed? I've been hunting through the section
> on naming and binding:
>
> http://docs.python.org/3/reference/executionmodel.html#naming-and-binding

The documentation appears to be wrong.  It says:

"""
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.
"""

But this only applies to function blocks, not the general case.  In
general, I believe it is more accurate to say that a variable is local
to the block if its name is found in the locals() dict.  That normally
won't be true until the variable has been bound.  Any references prior
to that will look for a global variable.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to