-3

So in Python you can do the following:

def append(_list):
    _list.append(1)

_list = [0]
append(_list)
print(_list)

This obviously allows me to append 1 to the list, however if I change the reference of list within the append function it doesn't change (which is as expected due to Pass-by-Object-reference). Java works similar however it's considered Pass-by-Reference. Does this simply mean that Python is like C++ when it passes a pointer that points to an object by value and then dereferences (sort of like when using C/C++'s -> operator)?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
DJ Poland
  • 955
  • 1
  • 9
  • 23
  • What do you mean in **Java works similar however it's considered Pass-by-Value.**? – Dennis Vash Jul 26 '18 at 01:03
  • Java passes the reference by value right? So you can manipulate the contents of the object but not the object's reference itself. – DJ Poland Jul 26 '18 at 01:04
  • Don't get too tangled up in what labels to use for the concepts. The important thing is that when you pass an object, Java and Python both pass a handle value, that the called code can use to refer to the actual argument. In Python even values of fundamental types like integers, are treated as objects. – Cheers and hth. - Alf Jul 26 '18 at 01:07
  • yes my bad : Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value. – Dennis Vash Jul 26 '18 at 01:08
  • Java object parameter passing and assignment semantics are essentially identical to Python's semantics. There just isn't a good, well-accepted name for those semantics. Java is pretty insistent on calling it pass by value, but calling it pass by value doesn't do anything to communicate the semantics without an explicit explanation. There are terms like "call by object" or "call by sharing", but they're too obscure to be useful for communication. – user2357112 Jul 26 '18 at 01:09
  • `[0, 1]` is your expected or not? Python really pass reference, any problem? – atline Jul 26 '18 at 01:38
  • There is no such thing as "C/C++". Simply use the mundane English word "and" if you want to refer to two things. –  Jul 26 '18 at 01:41

2 Answers2

3

In Python everything is a reference to the heap. In Java, you can have primitive types that live on the stack that aren't references to anything on the heap, but everything else is a reference to objects on the heap (even arrays of primitives).

During a function call, the reference (or primitive) is copied to the new stack frame in both Java and Python.

C can put complex structures, (like structs and arrays) on the stack. But there is no notion of stack objects that aren't references in Python. This is very different than in C, where you can not only copy an object to the new stack frame, but you can instead actually pass a pointer to an object that lives on an earlier stack frame.

gilch
  • 10,813
  • 1
  • 23
  • 28
-1

The important distinction in Python is not pass-by-value or -reference, but rather between mutable and immutable objects.

Everything is basically passed by reference but if the object does not allow you to modify it (it is immutable) you cannot change the original object which reference was passed. For example Python ints are immutable, thus even the int is passed by reference you can never modify the original obect. If you want to do this, you have to wrap the object in a mutable object, for example a list. Lists can be modified (append, pop, etc.)

So you "could" say that Pythons attribute access . is similar to C++s -> but that is kind of weird terminology for Python.

In your example you pass a mutable object (list). So the append will modify the original object. The output of your code is thus [0, 1]. (You should include that in your question probably.)

C. Yduqoli
  • 1,706
  • 14
  • 18