[Python-ideas] binding vs rebinding

spir denis.spir at free.fr
Fri Feb 6 11:31:45 CET 2009


Le Fri, 06 Feb 2009 08:51:14 +1100,
Steven D'Aprano <steve at pearwood.info> a écrit :

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

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

Good point, yes, this holds for dicts, too!

mydict['x'] := bar  # ==> 
KeyError: mydict['x'] does not exist.

mydict['x'] = 1  # create new key and bind it to 1

mydict['x'] := 2  # (syntax suggestion) rebind key to 2

mydict['x'] = foo  # ==> 
KeyError: mydict['x'] already exists.

> Also, your suggestion is conceptually the same as requiring declarations:
> 
> x = 1  # declare x with value 1
> x := 2  # assign to x

Hem... well, you can actually see it like that. The main difference is that, unlike in "declarative" languages, there is no declaration without binding.
var foo; int i		# not in python
So that I would rather call that 'initialization' (== declaration + first binding) as opposed to 'rebinding'.

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

Interesting, thank you. Now comes on stage the naming problem. What is here expressed is an optional additional step, right? This is written as a special case where whatever is symbolized with 'x' will take a specific value. Later the program enters back the main flow and this thing will have a standard value. I assert that the same name should not be used for both special and standard cases. This imo shows a lack of distinction. Using a different name will make things clearer:

if flag:         # 'flag' expresses a special case
     flag_x = 2	 # possibly reuse the case name as prefix
     print foo(flag_x)
x = 3  # is this a rebinding or a new binding?
print bar(x)

I am rather sure that in any concrete model, once the distinction is made clear, then obvious naming can be found.

if ambiguity:
	ambiguity_message = 'ambiguity warning: "%s"\n' % ambiguous_expr
	print warning_format(message)
message = "\t%s\n" % line_body
print standard_format(message)

Probably most name rebindings rather express a lack of distinction that makes the code obscure. Haven't you ever read things like:

config = "config.txt"	# string: name
config = open(config)	# file object
config = config.read()	# string: content
config = config.lines()	# array of strings
config = parse(config)	# e.g. custom object

Denis

------
la vida e estranya



More information about the Python-ideas mailing list