Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and Java parameter passing [duplicate]

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.

like image 906
orrymr Avatar asked Apr 21 '15 15:04

orrymr


1 Answers

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

like image 150
GriffeyDog Avatar answered Sep 29 '22 02:09

GriffeyDog