[Python-ideas] Fw: binding vs rebinding

spir denis.spir at free.fr
Thu Feb 5 19:45:38 CET 2009


Le Thu, 5 Feb 2009 16:12:17 +0000,
Mark Dickinson <dickinsm at gmail.com> a écrit :

> On Thu, Feb 5, 2009 at 1:22 PM, spir <denis.spir at free.fr> wrote:
> > Hello,
> >
> > I wonder why there is no difference in syntax between binding and rebinding. Obviously, the semantics is not at all the same, for humans as well as for the interpreter:
> > * Binding: create a name, bind a value to it.
> > * Rebinding: change the value bound to the name.
> >
> > I see several advantages for this distinction and no drawback.
> 
> How would you write code like:
> 
> my_result = []
> for item in items:
>     a = <some intermediate result computed from item>
>     ...
>     my_result.append(<something computed from a>)
> 
> where a is bound for the first time on the first iteration
> of the loop, and rebound on all subsequent iterations?

Yes, good point!

That's probably a reason why in some languages loops create local scopes/namespaces. In which case the issue disappears, as 'a' is newly created for each iteration. An incremental value (e.g. a sum) is first defined/initialized outside the loop instead.
I takes this as different, but related, issue; because the intention and meaning here is really to have an 'a' for each iteration, matching each item. It is not a value that will evoluate along with the loop cycles, as a sum would: it is not rebinding. Conceptually, 'a' is thus a loop-local name.
Maybe a special case for loops, where such "utility short-life names" are common? Not very nice.
If "a=..." is allowed even when 'a' exists, and recreates the name, then a distinction with rebinding still holds, in the sense that explicit rebinding is possible (including in non-local scope). But we lose the side-effect of getting a NameError in case the programmer errorneously types an existing name.



> Mark
> 


------
la vida e estranya


------
la vida e estranya



More information about the Python-ideas mailing list