[Python-ideas] binding vs rebinding

Steven D'Aprano steve at pearwood.info
Thu Feb 5 22:51:14 CET 2009


spir wrote:

> Hello,
> 
> I wonder why there is no difference in syntax between binding and rebinding. Obviously, the semantics is not at all the same, for humans as well as for the interpreter:

Denis, as you can see from the above line, your email breaks the 
Internet standard of using hard-line breaks within paragraphs. This 
causes problems for other people. Please set your mail client to wrap 
text at 68, 70 or 72 characters.

> * Binding: create a name, bind a value to it.
> * Rebinding: change the value bound to the name.
> 
> I see several advantages for this distinction and no drawback. The first advantage, which imo is worthful enough, is to let syntax match semantics; as the distinction *makes sense*.

In Python, names are stored in namespaces, which are implemented as 
dictionaries. There is a nice correspondence between the syntax of 
namespaces and of dicts:

x = 1  # create a new name and bind it to 1
x = 2  # rebind name to 2
del x  # delete name

mydict['x'] = 1  # create new key and bind it to 1
mydict['x'] = 2  # rebind key to 2
del mydict['x']  # delete key

Also, your suggestion is conceptually the same as requiring declarations:

x = 1  # declare x with value 1
x := 2  # assign to x


Finally, what should we do here?

if flag:
     x = 2
     print foo(x)
x = 3  # is this a rebinding or a new binding?
print bar(x)



-- 
Steven




More information about the Python-ideas mailing list