What's up with rebinding assignment?
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:
> 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
> 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:
x = 1
x = 2
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:
val = val + amount # I don't like +=, so there.
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.
> 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