2

I come from Java and C++, and now I'm learning Python with this tutorial.

To the best of my knowledge, in Java arguments are passed by value. It seems that they are passed by reference, because when the argument is an object, we pass the reference of the object. So, if inside the called function we change the object state, the object will result modified even after the function call. But, if inside the function we reassign the function parameter we are changing the referenced object, which means that from that point the function's argument will not be affected anymore.

A simple example:

void foo(List<Integer> l){
  l.append(1);  //affecting list_argument
  l = new ArrayList<int>();
  l.append(2);  //not affecting list_argument
}

public static void main(String[] args){
  List<Integer> list_argument = new ArrayList<Integer>();
  foo(list_argument); //passing list_argument's reference by value
  //list_argument contains 1 only
}

Now, I'm reading this article about passing arguments in Python. In the article, it refers to a fancy name strategy called "passing by object", but to me it seems exactly the mechanism that I described above.

So, my question is: is there any difference the passing argument strategy between Python and Java?

AntoineLB
  • 482
  • 3
  • 19
justHelloWorld
  • 6,478
  • 8
  • 58
  • 138
  • 3
    You have already got the point, there is no difference. Indeed, they are all pointers. – Sraw Sep 13 '17 at 09:17
  • @anuragal this is not a duplicate. In fact, in the linked question the word "java" doesn't appear neither in question or answers. I'm explicitly asking what is the difference between these two languages on the topic. – justHelloWorld Sep 13 '17 at 09:17
  • @Sraw thanks. I wonder why in the linked tutorial they do not mention java at all (but they link C several times) – justHelloWorld Sep 13 '17 at 09:18
  • @justHelloWorld As you already mentioned that you know how Java and C++ works then you only need to know how Python works, rest you can figure it out right? – anuragal Sep 13 '17 at 09:20
  • @anuragal if I know a language, it doesn't mean that I know all the details about it, or that I cannot be wrong. That's why I say "to the best of my knowledge", because (sine I'm human), my knowledge on the topic could be miss something or could be wrong. Anyway, I'm sure that this question is useful to many programmers who come from Java and want to learn Python. – justHelloWorld Sep 13 '17 at 09:22
  • This topic has already been discussed: https://stackoverflow.com/questions/29776736/python-and-java-parameter-passing – Tomasz Plaskota Sep 13 '17 at 09:25
  • 1
    Because when it comes to some value type such as `int`. There is also some difference. If you try to check the type of `a` which assigned to `5` (`a=5`), you will find it is also a class. In this case, it becomes complex. – Sraw Sep 13 '17 at 09:27
  • Also it is like java but not always. It is called differently, because it acts differently in subtle ways, but still it differes so they use different name not to confuse someone comming from Java. Example case would be treating mutable and immutable objects. – Tomasz Plaskota Sep 13 '17 at 09:30

0 Answers0