[Python-Dev] replacing 'global'

Alex Martelli aleaxit at yahoo.com
Sun Oct 26 10:23:54 EST 2003


On Sunday 26 October 2003 13:37, Just van Rossum wrote:
> Skip Montanaro wrote:
> > Nope.
>
> Ok :). Yet I think I'm starting to agree with you and Alex that :=
> should mean "this name is NON-local".

The more I think about it, the more I like it in its _simplest_ form.

> A couple more things:
>
> - I think augmented assignments CAN be made "rebinding" without breaking
> code, since currently a += 1 fails if a is neither local nor global.

You are right about the breaking code, but I would still slightly prefer
to eschew this just for simplicity -- see also below.

> - Would := be allowed in statements like "self.a := 2"? It makes no
> sense, but since "(a, b) := (2, 3)" IS meaningful, what about
> "(a, b, self.c) = (1, 2, 3)"?

I would not allow := in any but the SIMPLEST case: simple assignment
to a bare name, no unpacking (I earlier said "no packing" but that's silly
and I mispoke there -- "a := 3, 4, 5" WOULD of course be fine), no
chaining, no := when the LHS is an indexing, slicing, attribute access.

Keeping := Franciscan in its simplicity would make it easiest to implement,
easiest to explain, AND avoid all sort of confusing cases where the
distinction between := and = would otherwise be confusingly nonexistent.
It would also make it most effective because it always means the same
thing -- "assignment to (already-existing) nonlocal".  This is much the
spirit in which I'd forego the idea of making += etc access nonlocals too,
though I guess I'm only -0 on that; it seems simplest and most effective
to have the one concept "rebinding a nonlocal name" correspond in strict 1-1
way to the one notation := .  Simplicity and effectiveness feel very 
Pythonic to me.

I think rebinding nonlocals should be rare enough that the fact of having
to write e.g. "a := a+1" rather than "a += 1" is a very minor problem.  The
important use case of += & friends, "xop[flap].quip(glop).nip[zap] += 1",
gets no special benefit from += being deemed "rebinding" -- the rebinding
concept applies usefully to bare names, and for a bare name writing
name := name <op> RHS
is no big deal wrt
name <op>= RHS

If name's a huge list, name.extend(anotherlist) is a fine substitute for
name += anotherlist if you want to keep name nonlocal AND get some
efficiency gain.  Other containers with similar issues should also always
supply a more readable synonym to __iadd__ for such uses, e.g. sets
do, supplying union_update.

So, keeping += &c just like today seems acceptable and preferable.


Alex




More information about the Python-Dev mailing list