What's up with rebinding assignment?

John Roth johnroth at ameritech.net
Fri Mar 21 16:27:37 CET 2003


"Beni Cherniavsky" <cben at techunix.technion.ac.il> wrote in message
news:mailman.1048248875.10571.python-list at python.org...
> Once upon a time, Just threw in a great (IMHO ;-) idea on pytohn-dev:
>
> http://mail.python.org/pipermail/python-dev/2003-February/032764.html
>
> The idea was to add am ``:=`` rebinding operator, one that changes a
> visible binding without making it local to the current scope::
>
>     def bind():
>         x = 1
>         def rebind():
>             x := 2
>         rebind()
>         print x
>
> would print 2.
>
> Futhermore, augmented assignments can become always rebinding with
> almost no breakage of code::
>
>      def bind():
>          x = 1
>          def rebind():
>              x += 1
>          rebind()
>          print x
>
> Currently this makes `x` local to `rebind` and raises an exception
> because `x` is not initialized there; so no working code can change
> its meaning (unless you do evil locals() manipulations perhaps).
>
> Using ``:=`` (but not augmented assignments) for names that are
> already local to the current scope should probably be forbidden -- it
> adds nothing and somebody would think it gives write-only access to
> shadowed variables.
>
> This seems a very sound addition that will remove the need for the
> ugly singleton-list hack::
>
>      def bind():
>          x = [1]
>          def rebind():
>              x[0] += 1
>          rebind()
>          print x[0]
>
> My question is: is there a chance this will make it into Python?
> Nobody seems to have noticed it...  Will a PEP help?

Two (or maybe three) thoughts.

1. I'd be very strongly against it, unless it was limited to static
scoping.
I can think of uses for this, but allowing any function to modify
variables
further up in the calling chain promiscuously is asking for trouble.
Limiting it to static scope reduces the possibility of it causing
trouble. That does, of course, mean that it could only be used in an
embedded function, and that the embedded function couldn't be
passed out to the caller.

2. I'm not certain I like the syntax.

3. I like the notion of an abbreviated notation for rebinding things
at the instance or module level, however.

By all means, submit a PEP. That's the formal way of getting things
into the pipeline.

John Roth

>
> --
> Beni Cherniavsky <cben at tx.technion.ac.il>
>






More information about the Python-list mailing list