Nested scopes, and augmented assignment

Antoon Pardon apardon at forel.vub.ac.be
Thu Jul 6 15:08:41 EDT 2006


On 2006-07-06, Piet van Oostrum <piet at cs.uu.nl> wrote:

>>AP> Aren't we now talking about implementation details? Sure the compilor
>>AP> can set things up so that local names are bound to the local scope and
>>AP> so the same code can be used. But it seems somewhere was made the
>>AP> decision that b was in the local scope without looking for that b in
>>AP> the scopes higher up.
>
> Yes, as I (and others) have already said several times: an assignment to a
> variable inside a function body (but not an assignment to an attribute or
> part of an object) without a global declaration makes that variable a local
> variable. That is not an implementation detail; it is part of the language definition.

You seem to think I didn't understand this. Maybe I'm not very good
at explaining what I mean, but you really haven't told me anything
here I didn't already know.

>>AP> [ ... ]
>>AP> Now we come to the line "b = a"
>
>>AP> This could be then executed internally as follows:
>
>>AP>   LeftScope = SearchName("b", ScopeList)
>>AP>   RightScope = SearchName("a", ScopeList)
>>AP>   LeftScope["b"] = RightScope["a"]
>
>>AP> But I don't have to do it this way. I already know in which scope
>>AP> "b" is, the local one, which has index 0. So I could just as well
>>AP> have that line exucuted as follows:
>
>>AP>   LeftScope = ScopeList[0]
>>AP>   RightScope = SearchName("a", ScopeList)
>>AP>   LeftScope["b"] = RightScope["a"]
>
>>AP> As far as I understand both "implementations" would make for
>>AP> a correct execution of the line "b = a" and because of the
>>AP> second possibility, b is IMO not conceptually searched for in
>>AP> the same way as a is searched for, although one could organise
>>AP> things that the same code is used for both.
>
> That is the optimization I spoke of above. But it is not the problem we
> were discussing.

Could you maybe clarify what problem we are discussing? All I wrote
was that with an assignment the search for the lefthand variable
depends on whether the lefthand side is a simple variable or
more complicated. Sure people may prefer to speak about (re)binding
vs mutating variables, but just because I didn't use the prefered terms,
starting to doubt my understanding of the language, seems a bit
premature IMO. I'm sure there are areas where my understanding of
the language is shaky, metaclasses being one of them, but understanding
how names are searched doesn't seem to be one of them.

-- 
Antoon Pardon



More information about the Python-list mailing list