4 ms·
"Pass by value" and "pass by reference" are both wrong with respect to Python. They denote semantics that Python does not have, and coders believe and do wrong
by colanderman 8y ago
"Pass by value" and "pass by reference" are both wrong with respect to Python. They denote semantics that Python does not have, and coders believe and do wrong things if they believe Python follows one or the other. Hence a 3rd term is needed. I don't care what you call it, because it's just a name, but it is a distinct idea, and I'm not the only one who thinks this: https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sh...
> In Python, f can change the value of a
but it can't. `f` can't change the object which `a` references:
def f(x): x = 5
is a function with no visible effect. When called as `f(a)`, `a` retains whatever value it had previously.
People who believe Python is "call-by-reference" don't understand this and write incorrect or overcomplicated code as a consequence. The distinction is important.
- lmm 8y ago> They denote semantics that Python does not have There is no consensus that "pass by reference" denotes that, whatever a wikipedia editor says. Normal working programmers do not understand "pass by reference" to mean specifically "pass a reference to the caller's stack" but only the more general concept of "pass a reference to the value of a". If you want to talk specifically about what kind of reference gets passed, you need some new terms for that, and both the new terms you come up with will be subtypes of what most of the world will continue to understand as a general category of "pass by reference". No-one outside of these arguments on HN gets confused about whether you can write a swap function in Python; people understand "pass by reference" to mean something more general and entirely true of Python (and Java and so on). You can tell by how often we see those languages described as "pass by reference". > but it can't. `f` can't change the object which `a` references: Yes it can. It can't make a to refer to a different object (as your f tries to), but it can change the object a refers to just fine.