[Python-Dev] replacing 'global'

Alex Martelli aleaxit at yahoo.com
Sun Oct 26 05:34:56 EST 2003

On Sunday 26 October 2003 01:09, Just van Rossum wrote:
> It seems noone liked (or remembered) an idea I proposed last february,
> but I'm going to repost it anyway:
> How about adding a "rebinding" operator, for example spelled ":=":
>    a := 2
> It would mean: bind the value 2 to the nearest scope that defines 'a'.

In the light of the current discussion, this looks beautiful.  At least if
compresence of := and other bindings (= , class, def, for, import, ...)
for the same name at the same scope is flagged as an error.

I would also suggest for simplicity that := be only allowed in the
simplest form of assignment: to a single bare name -- no packing,
unpacking, chaining, nor can the LHS be an indexing, slicing, nor
dotted name.

> Advantages: no declarative statement (I don't like global much to begin
> with, but much less for scope declarations other that what it means
> now). It's a nice addition to the current scoping rule: an assignment IS
> a scope declaration.

Yes.  Neat. := becomes an unobtrusive but unmistakable indication
"I'm binding this name in NON-local scope" and -- if defined with the
restrictions I suggest -- meets all of my issues wrt 'global'.

> Possible disadvantage: you can only rebind to the nearest scope that
> defines the name. If there's a farther scope that also defines that name
> you can't reach that. But that's nicely symmetrical with how _reading_
> values from nested scopes work today, shadowing is nothing new.

I agree.  Reaching other scopes but the "closest" outer one is not a
use case of any overriding importance, IMHO.

> Ideally, augmented assignments would also become "rebinding". However,
> this may have compatibility problems.

Unfortunately yes.  It might have been better to define them that way in
the first place, but changing them now is dubious.  Besides, we could
not load them with the restrictions I think should be put on := to make
it simplest, sharpest, and most useful.


More information about the Python-Dev mailing list