[Python-Dev] Scoping vs augmented assignment vs sets (Re: 'fast locals' in Python 2.5)

Boris Borcic bborcic at gmail.com
Wed Jun 14 12:05:26 CEST 2006


Terry Reedy wrote:
> "Boris Borcic" <bborcic at gmail.com> wrote in message 
> news:e6mcfg$iao$1 at sea.gmane.org...
> 
>> being transformed to profit from simplifications I expected sets to allow.
>> There, itemwise augmented assigments in loops very naturally transform to
>> wholesale augmented assignments without loops. Except for this wart.
> 
> Your transformation amounted to switching from collection mutation to 
> object rebinding.  In Python, that is a crucial difference.

Ok, that is a crucial difference. The question becomes : is that difference in 
the case of augmented assignment maintained for practical or for purity aka 
ideological reasons ?

> That the 
> mutation and rebinding were both done with augmented assignments is not 
> terribly important except as this masks the difference.  When *you* read
> your code, you know that you will only call the inner function with a 
> mutable collection object, so you know that the name will be rebound to the 
> same object after mutation, so you can think of the augmented assignment as 
> being the same as collection mutation.

Not quite. When I read my code (and while I transform it) I know (as an 
invariant) that nowhere in my function do I initially bind a value to my 
variable, and I know that it can't be done with augmented assignments, and I 
know that no working code can /ever/ result from letting a function-local scope 
capture a name /just/ because it is the target of an augmented assignment.

So, when despite this obvious certainty the compiler translates my code to 
something that can't possibly run while it happily translates x+=1 and x=x+1 to 
different bytecodes with no qualms about x possibly being bound to an immutable, 
well, I do feel victim of something like a hidden agenda.

> 
> But the compiler does not know any such thing about the target of the 
> augmented assignment and must therefore treat the statement as an 
> assigment.  It was a bug for a2 to do otherwise, even though the bug was 
> locally optimal for this particular usage.

I must thank you for the effort of elaborating a polite and consistent 
explanation that was almost to the point and believable.

Regards, Boris Borcic
--
"On naît tous les mètres du même monde"



More information about the Python-Dev mailing list