Nested scopes, and augmented assignment

Antoon Pardon apardon at forel.vub.ac.be
Sun Jul 9 12:05:50 EDT 2006


On 2006-07-08, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
> On 8 Jul 2006 18:52:56 GMT, Antoon Pardon <apardon at forel.vub.ac.be>
> declaimed the following in comp.lang.python:
>
>
>> 
>> I'm not fooled by that phrase. I just think the mutate vs rebind
>> explanation is not complete.
>> 
>> If we have two statements "a = b" and "c.d = b" the fact that a is being
>> rebound while c is mutated doesn't explain why we allow c to be searched
>> out of the local scope.
>> 
> 	The "search" is not different, per se... It is only after the object
> is found that the difference becomes apparent -- a rebinding changes an
> object's ID, and is not permitted for a non-local UNLESS a "global"
> statement appears before any usage of the name.
>
> 	"c.d =..." has absolutely no effect on the ID of object C; in that
> aspect it is a read-only look-up of "c". IOWs, the same look-up as would
> be used if "c" were on the RHS of the statement.
>
>> be searched in local space so that code like the following would
>> throw: UnboundLocalError: local variable 'c' referenced before assignment
>> 
>> c = SomeObject
>> def f():
>>   c.a = 5
>>
> 	What would you say the behavior should be for:
>
> 	c.a = c.b
> vs
> 	la = c.b
>
> 	The look-up of "c" is the same on both sides of the statement; local
> (not found) then global (found), THEN the operation is applied. On both
> sides "c" is a read-only look-up (that is, no changes to the ID of "c"
> -- no rebinding -- take place).
>
> 	Are you suggesting we need to use "global c" in order to have "c.b"
> on the RHS? By that logic, we would also need "global sys" to reference
> "sys.argv" inside a function definition. Remember -- the modules loaded
> by "import" are just more "SomeObject" samples...

I think you are misunderstanding what I was getting at. This example was
not meant to illustrate how I think python should behave. I thought I
had made that clear.

When someone gets confused over the difference between rebinding or mutating
a variable on an intermediate scope, the explanation he mostly seems to get
boils down to: one is rebinding, the other is mutation, this is a fundametal
difference in python.

My impression is that they seem to say that the fundamental difference
between mutation and rebinding implies the specific behaviour python
has now. IMO this explanation is incomplete. The python developers
could have chosen that a line like 'c.a = ...' would have resulted
in c being included in the local scope. Then rebinding and mutation
would still be fundamentally different from each other but the specific
confusion over why 'k[0] = ...' worked as expeced but 'k = ...' didn't,
will disappear.

So I only used this example as an illustration why I think the usual
explanation is not completed. This example is not to be taken as an
illustration of how I think python should behave.

-- 
Antoon Pardon



More information about the Python-list mailing list