4 ms·
7 Isn't Python actually pass-by-value / pass-pointer-to-object-by-value like Java is? Many confuse pass-pointer-to-object-by-value with pass-by-reference, but t
by pkolaczk 8y ago
7 Isn't Python actually pass-by-value / pass-pointer-to-object-by-value like Java is? Many confuse pass-pointer-to-object-by-value with pass-by-reference, but they are different things. Pass-by-reference allows to write a procedure that swaps two external values.
- clumpthump 8y agoPeople confusing this terminology is one of my pet peeves. And it's not a petty one. It often impedes the conversation they're trying to have.
- civility 8y agoYou're right that you can't perform swaps with a Python function, but it's never pass-by-value like Java primitive types. It feels like pass-by-value because int, long, float, complex, and string are basically immutable.
- szatkus 8y agoI may be missing something, but I can't find any example in my mind which indicates difference between passing in Java and Python. And it's pass-by-value for all types, because for objects an address is the value.
- civility 8y agoJava primitives (short, float, double, etc...) are copied to the calling function, but arrays and objects are passed by reference. This is easier to explain in a language like C++, which can do either: // 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.
- szatkus 8y agoMy C++ is a bit rusty, but AFAIR `void f(Object& o) { ... }` would allow you to replace the whole object in passed variable (`o = o2`). You can't do this in Java, you can only modify its fields. It's more like `void f(Object* o) { ... }`
- civility 8y agoYou're right, it is more like a pointer. In C++, that would call the assignment operator which can do anything it wants, but the address of the Object would not be replaced. Java would change the value of the pointer in the callee. These cross-language comparisons have too many nuances for me to try and make simple examples. Really, my original point is that CPython does not have "primitive types" as in Java. Everything is passed by pointer to the PyObject.
- baddox 8y ago> Java primitives (short, float, double, etc...) are copied to the calling function, but arrays and objects are passed by reference. 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."
- civility 8y ago> "Pass by reference" is a specific term for a much different technique which is very rarely encountered these days. C++ isn't all that rare. I suspect we'll have to agree to disagree whether your definition of pass by reference is universal. Given your definition, can you name any language with calling semantics which are not pass-by-value? I mean, you're always passing the value of something (be it the actual value, the address of the memory where the data is stored, or the pointer to the string containing the name of the data in an associative array).
- blattimwind 8y ago> , but it's never pass-by-value like Java primitive types That's because this class of types doesn't exist in Python.
- jonnycomputer 8y agoditto