[Python-Dev] replacing 'global'

Just van Rossum just at letterror.com
Sun Oct 26 04:53:18 EST 2003


Guido van Rossum wrote:

> Hardly arbitary (I have fond memories of several languages that used
> :=).

I think augmented assignment should (ideally) also be rebinding, and :=
kindof looks like an augmented assignment, so I don't think it's all
that bad. I'd be used to it in a snap.

But: let's not get carried away with this particular spelling, the main
question is: "is it a good idea to have a rebinding assignment
operator?" (regardless of how that operator is spelled). Needless to
say, I think it is.

> But what is one to make of a function that uses both
> 
>   a := 2
> 
> and
> 
>   a = 2
> 
> ???

Simple, "a = 2" means 'a' is local to that function, so "a := 2" will
rebind in the same scope. So the following example will raise
UnboundLocalException:

def foo():
    a := 3
    a = 2

And this will just work (but is kindof pointless):

def foo():
    a = 2
    a := 3

And this would be a substitute for the global statement:

a = 2
def foo():
    a := 3

(Alex noted in private mail that one disadvantage of this idea is that
it makes using globals perhaps TOO easy...)

Just



More information about the Python-Dev mailing list