by pkolaczk · 7y ago · 0 comments
And it's pass-by-value for all types, because for objects an address is the value.
// Java always does these:
void f(Object& o) { ... } // passes objects by reference
void g(double x) { ... } // primitives are copied
// Java can not do these:
void p(Object o) { ... } // copy of the object
void q(double& x) { ... } // reference to primitive
CPython always passes by reference (pointer), and a C extension can break the rules and modify the otherwise immutable types (int, float, complex, string, etc...).You can squint and say everything is pass-by-value because you can always pass pointers, but then we're really losing any meaningful definitions for what semantics we're describing. I mean, it's all pass-be-electron if you dive deeply enough.
Not really. "Pass by reference" is a specific term for a much different technique which is very rarely encountered these days. It means that if you pass a variable "foo" to a function, that function can assign "foo" to some other value and the value of "foo" will also be changed outside of that function.
The confusion, I think, is that true pass by reference is almost never encountered in modern programming languages and courses, and some people mistakenly use the term "pass by reference" when explaining the difference between e.g. primitive types and objects in Java.
Java passes everything by value. The value of a variable assigned to an object is indeed a "reference" to that object in memory, but that's a coincidental use of the term "reference."
It's more like `void f(Object* o) { ... }`
That's because this class of types doesn't exist in Python.