0

I have a code as below,

attractions = [[] for i in range(5)]

def add_attraction(attraction):
  attractions_for_destination = attractions[2]
  attractions_for_destination.append(attraction)
  return 

pass by reference occurs in 3rd line "attractions_for_destination = attractions[2]". The general idea of pass by reference that I have is that it happens between actual parameter and formal parameter. But the above line is a simple assignment code. Can pass by reference happen in a simple assignment code?

If so, if I write

attractions_for_destination = attractions[2] + ": best attraction site"

will altering the value force the newly made variable to use a new memory space?

Sean
  • 57
  • 1
  • 5
  • 1
    Welcome to SO! I'm not really sure what's being asked here or if I follow the question. Python never passes by reference, it's pass by value, for starters, so I don't understand what an assignment to an array element has to do with a new memory space. Sure, the variable param references an object and changing the value of an element like this changes memory, but that seems sort of trivial. Can you clarify? – ggorlen Jun 01 '20 at 02:19
  • 1
    **Python never uses pass by reference**. Never. Python uses *a single evaluation strategy* and it is *neither call by reference nor call by value*. It uses ["call by object sharing"](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing), although it goes by a lot of different names, sometimes you hear "call by assignment" as well. – juanpa.arrivillaga Jun 01 '20 at 04:16
  • @ggorlen no absolutely not, Python is not pass by value. If it were, arguments would be *copied*. If I did: `def foo(x): x.append(42)` then `y = []; foo(y); print(y)` would print `[]` – juanpa.arrivillaga Jun 01 '20 at 04:18
  • The value in this case is an object reference, much like Java, which is pass by value but objects are never copied in calls. The variable _is_ copied, but it's a reference to an obj. `x` is local to the stack frame--if you reassign `x` to something else, it stops "pointing" to the object it represents. We definitely have 2 vars here, `x` and `y`, both of which point to the same obj. In no way am I suggesting that the code you show would ever be possible in Python, and that's not my understanding of pass by value at all. – ggorlen Jun 01 '20 at 04:36
  • 1
    I think we're saying the same thing (from linked article): "However, the term "call by sharing" is not in common use; the terminology is inconsistent across different sources. For example, in the Java community, they say that Java is call by value." This is exactly what I mean. – ggorlen Jun 01 '20 at 04:41

1 Answers1

2

Saying pass by reference or pass by value isn't really applicable to python. For example consider Java. Everything in Java is pass by value, everything. However the value of an object is the reference to the object. So it is, in a way, pass by reference since mutating the object will be reflected to the calling context. Calling it "pass by value" is a bit confusing since for people used to C-family (where this distinction is more straightforward) assume that pass by value means you won't modify the underlying argument passed.

So saying Java is "pass by value" is true but possibly confusing. It is a more relevant statement at much lower levels of the language (like when talking about how the compiler handles passing arguments but not at the level of application programmer).

Python is similar. The confusion about the Java example actually lead Python to not say if it is pass by value/reference. http://stupidpythonideas.blogspot.com/2013/11/does-python-pass-by-value-or-by.html

So for programming, it is save to assume that any object is "passed by reference" (technically no but essentially yes). It is more complicated than that but that link explains it in more depth.

Garrigan Stafford
  • 1,331
  • 9
  • 20
  • 1
    I think the problem with Java is you have this huge wart of a distinction between "primitive types" and "reference types". Python is purely object-oriented, so everything is an object. Everything is "a reference type". The most important thing people need to understand is that evaluation strategy isn't a dichotomy between call by reference and call by value, there are *several different alternatives*. I would say Python *never* uses pass by reference. If it did, you could do something like `def foo(&x): x = 42; y = 0; foo(y); print(x)` and you'd get 42. Can't do that in Python. – juanpa.arrivillaga Jun 01 '20 at 04:11