What's up with rebinding assignment?

Beni Cherniavsky cben at techunix.technion.ac.il
Fri Mar 21 07:13:42 EST 2003


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?

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





More information about the Python-list mailing list