Call by binding [was Re: [Tutor] beginning to code]

Steve D'Aprano steve+python at pearwood.info
Sun Sep 24 20:49:39 EDT 2017


On Mon, 25 Sep 2017 01:56 am, bartc wrote:

>> The point I am making is that we could describe just about any and all
>> languages with functions "call by binding", whether they are call by value
>> like C, call by reference like Fortran, call by need like Haskell, or call by
>> sharing like Python.
>
> Then 'binding' is either ill-defined or used wrongly.

No, binding is a perfectly fine word. It is just too generic to be helpful here.

In Pascal or C:

a := b;
a = b;

the compiler binds a copy of the value*of b to the identifier a. In C++ were we
have reference variables:

a = b;

will either bind a copy of b to a, same as C, or bind a reference to b to a
(thus making a an alias to b).

In Python:

a = b

binds the value of b to a, without copying.

If it weren't too late to change decades of use, I'd say that "pass by value"
should be called "pass by copying".

The nature of the binding can differ: Pascal and C use the "variables are a
numbered box" model, while Python uses the "variables are one end of piece of
string tied to the object" model.

Name binding is a generic term, like assignment, which doesn't really hint at
how the assignment is done. All it really means is that the R-value (the right
hand side of the assignment, an expression or literal or value of some sort) is
associated with the L-value (the left hand side of the assignment, a variable
name, or identifier, of some sort).

By the way, the model of "pieces of string" to describe Python's assignment
model leaks too. (All models leak, to a lesser or greater degree.) The problem
with the model is that if you tied a piece of string to an object, the object
can follow the string backwards to find out its name (or names). But in Python,
objects cannot do that: they have no way of telling what names they are known
by, or even how many names there are.

(There may be clever tricks you can play with the garbage collector or debugger,
but that's not part of Python-the-language, that's a compiler-specific hook
which can peer into the implementation.)


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list