According to this tutorial, Python uses "Pass By Reference."
They then go on to give the following example. On what planet is this "Pass By Reference"? It looks like a clear cut case of "Pass By Value" to me.
Thoughts?
def changeme( mylist ):
mylist = [1,2,3,4];
print "Values inside the function: ", mylist
return
mylist = [10,20,30];
changeme( mylist );
print "Values outside the function: ", mylist
The parameter mylist is local to the function changeme. Changing mylist within the function does not affect mylist. The function accomplishes nothing and finally this would produce the following result:
# Values inside the function: [1, 2, 3, 4]
# Values outside the function: [10, 20, 30]
Update, Conclusion:
per answer by viraptor
below, "according to the docs, python is pass-by-value". So the tutorial is clearly wrong.
Python is not "call by reference", it is "call by sharing". This means a reference can be passed to a local subroutine, updated by the subroutine, and the update stays local to the subroutine and doesn't automatically affect the global value of the reference.
It is neither. It is call by sharing. I've also heard the term "pass by reference value" used.
Also known as "call by object" or "call by object-sharing," call by sharing is an evaluation strategy first named by Barbara Liskov et al. for the language CLU in 1974. It is used by languages such as Python, Iota, Java (for object references), Ruby, JavaScript, Scheme, OCaml, AppleScript, and many others. 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, whereas in the Ruby community, they say that Ruby is call-by-reference, even though the two languages exhibit the same semantics. Call by sharing implies that values in the language are based on objects rather than primitive types, i.e. that all values are "boxed".
The semantics of call by sharing differ from call by reference in that assignments to function arguments within the function aren't visible to the caller (unlike by reference semantics), so e.g. if a variable was passed, it is not possible to simulate an assignment on that variable in the caller's scope. However, since the function has access to the same object as the caller (no copy is made), mutations to those objects, if the objects are mutable, within the function are visible to the caller, which may appear to differ from call by value semantics. Mutations of a mutable object within the function are visible to the caller because the object is not copied or cloned — it is shared.