What's up with rebinding assignment?

Des Small des.small at bristol.ac.uk
Fri Mar 21 14:53:25 CET 2003


Beni Cherniavsky <cben at techunix.technion.ac.il> writes:

> 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.

I dislike this syntax a great deal, but I have pined for an
outer-scope mutating facility ever since scopes went lexical.  There
are many idioms (in, say, Lisp, but don't say that too loud) that
naturally exploit such a device - it's at the heart of what a lot of
people mean by 'closures'.

Since 'global' already exists to declare non-default scope for a
variable, I would propose the alternative, with your example:

def bind():
    x = 1
    def rebind():
        lexical x
        x = 2
    rebind()
    print x

where 'lexical' just hooks to the innermost use of a variable that
isn't itself declared lexical.

I wouldn't use it here though, I'd use it for things like:

def accumulator(val=0):
    def inner_acc(amount):
        lexical val 
        val = val + amount # I don't like +=, so there. 
        return val
    return inner_acc(val)

Since I don't expect the Python Priesthood (Bot-hood?) to be pleased
about this, I would want to market it as a less harmful version of
'global'.   It does do much the same thing, after all, and has much
the same conventions.

> My question is: is there a chance this will make it into Python?
> Nobody seems to have noticed it...  Will a PEP help?

I don't know how the politics works, but Alex Martelli has said that
the best way to get stuff in is to make an idea visible and hope
that Someone Important adopts it like unto their own brainchild.  

Naively, if we hang around discussing this ("Operator, Lisp luser!"
"Keyword, C++ buffoon!") for a bit maybe we'll attract some attention.

Des.

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

-- 
Des Small / Scientific Programmer/ School of Mathematics /
University of Bristol / UK / Word falling / Image falling




More information about the Python-list mailing list