15

I've seen in several places, including the Python documentation that Python uses pass by "assignment" semantics. Coming from a Java background, where the common mistake of saying "Java passes primitives by value, and objects by reference" is as a result of having objects references passed by value, I can't help but wonder if Python is really doing the same thing.

To me, the concepts of passing object references by value and pass by assignment seem identical. Is Python's use of the term "pass-by-assignment" an attempt to mitigate the problem of having erroneous statements such as the one I described above? If so, is it fair to say that parameter passing works in a similar way in the two languages?

Edit: I don't think that this question is a duplicate. Here I'm asking about the terminology used by Python, with direct reference to how Java does things. The other question is about whether the language is pass-by-value or pass-by-reference. I know that pass by assignment is the nomenclature used here, but my contention, which seems to be supported by the accepted answer, is that this is really no different to how Java does things; it's just a different name.

orrymr
  • 2,264
  • 4
  • 21
  • 29
  • Yes that's fair to say. But keep in mind, everything is an object in Python, there are no "primitives". But you're right in that everything is passed by value. Pass by reference is a concept found in pointer-based languages like C/C++. @Luiggi The OP said that it was a *common mistake* to say such. – Shashank Apr 21 '15 at 15:42
  • 2
    @LuiggiMendoza 'where the **common mistake of saying** "Java passes primitives by value, and objects by reference"' – Anderson Vieira Apr 21 '15 at 15:42

1 Answers1

13

Yes Python seems to be equivalent to Java in this respect. From http://learnpython.pbworks.com/w/page/15956522/Assignment :

But you must be careful about what is meant by "changes to parameters". Assigning a new value to a parameter name (inside the function, a parameter is just a local variable) does not change the original object--it only rebinds the local variable to a new object.

So, Python passes "object references" "by value".

GriffeyDog
  • 8,186
  • 3
  • 22
  • 34
  • 1
    Just to clarify: Python actually passes parameters by a reference to it. Assignment to the formal argument name will overwrite the previous reference. Try `l.append("Hello")` inside a function `f(l)`. The original object will get modified. In this aspect, Python is more similar to C++' references. – too honest for this site Apr 21 '15 at 16:03
  • 3
    @Olaf That's exactly what I mean to be saying, and Java behaves the same way. The object of the reference can be modified, but reassigning the reference within the function/method doesn't affect the caller's reference. – GriffeyDog Apr 21 '15 at 16:17
  • Yes, I just wanted to clarify that, because I did read your answer first differently, until I tought a bit about the quotes. Just was not sure if I interpreted your answer correctly. – too honest for this site Apr 21 '15 at 16:54
  • @Olaf: "In this aspect, Python is more similar to C++' references. " No. The semantics of passing and assigning in Python is exactly identical to that in Java, which is exactly identical to pointers-to-objects in C++. "C++ references" are something completely different. – newacct Apr 22 '15 at 00:54
  • @newacct: I disagree, as both are basically pointers, but with a different syntax and references hiding some of the pointer aspects. However, as it is difficult for me to explain exactly what I have in mind about that in english, I would say, we should just agree that we might disagree in that aspect. – too honest for this site Apr 22 '15 at 03:20
  • 1
    It is true that Java references are just pointers (as evidence from NullPointerException), but they are different to C++ pointers. I'm not sure of the exact details, but it's to do with assigning objects to pointers or object references within a method. In C++ if you reassign within the method, this will affect the outer scope, whereas with Java/Python, it won't. – orrymr Apr 22 '15 at 07:28
  • @Olaf: I am talking about exactly the syntax and semantics. It's equivalent to C++ pointers. It's NOT equivalent to C++ references at all. – newacct Apr 22 '15 at 18:37
  • @orrymr: Nope, they are exactly equivalent to C++ pointers, in that the same code with the same structure will always have the same behavior on both. Whatever you say about references in Java/Python also apply to pointers in C++ and vice versa. – newacct Apr 22 '15 at 18:39
  • @newacct: So, when I enter: Cat myCat; Does that make myCat a pointer? Only once I enter: myCat = new myCat("Gorlax the Destroyer"); That points the pointer (or Java reference) myCat to the new Cat object? – orrymr Apr 23 '15 at 08:31
  • 1
    @orrymr: Yup, exactly – newacct Apr 23 '15 at 21:50