3

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?

8
  • 3
    You have already got the point, there is no difference. Indeed, they are all pointers. Commented Sep 13, 2017 at 9:17
  • 1
    @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. Commented Sep 13, 2017 at 9:17
  • @Sraw thanks. I wonder why in the linked tutorial they do not mention java at all (but they link C several times) Commented Sep 13, 2017 at 9: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? Commented Sep 13, 2017 at 9:20
  • 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. Commented Sep 13, 2017 at 9:27

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.