A question on modification of a list via a function invocation

Steve D'Aprano steve+python at pearwood.info
Sun Sep 3 22:58:33 EDT 2017


On Mon, 4 Sep 2017 12:20 pm, Chris Angelico wrote:

> This is another proof that you can't divide everything into "pass by
> value" vs "pass by reference", unless you mess around with "passing a
> reference by value" or other shenanigans. In C, a string is not an
> entity; it's simply an array of characters. Arrays are never passed by
> value; yet everything in C is passed by value. So you pass a
> pointer... by value.

Indeed. And we say that in C, arrays are not first-class entities. We might say
that in C, arrays (and strings) are not actually values -- only the pointer to
the first slot in the array is a value.

The difference[1] between C and Python is that *top level Python code* abstracts
away the pointer passing that goes on behind the scenes inside the interpreter.
Your Python code treats strings and arrays (lists or tuples or even actual
arrays) as first class values, while C does not.

If you want a Python function to accept a string as argument, you simply declare
the parameter (with or without optional type hint), and pass the string as
needed. In C (as far as I understand it, correct me if I'm wrong) you cannot.
You must declare the parameter as a *pointer* type, and explicitly pass a
pointer to the start of the array, not the array itself.

That makes arrays (and strings) in C a bit of an odd corner case, and an
exception to the usual rules, like unboxed machine types in Java. We should
acknowledge them, but as exceptional cases, and we should note that Python has
no similar exceptional cases. All values in Python are first-class, and all are
passed in precisely the same way.



> What would you define LISP's semantics as? Pass by value? Pass by
> reference? Pass by name? Pass by immutability? Pass the salt?

My understanding is that LISP has more-or-less the same calling mechanism as
Python, only it had it long before Barbara Liskov gave a name to it when
describing CLU.






[1] What, only one? *wink*


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