As far as I am aware, python is generally referred to as 'call-by-sharing', but does it implement this with call-by-value (like Java) or call-by-reference? or something else? I would appreciate if this could be answered with official python documentation (in which I can't seem to find the answer) as opposed to anything subjective.
In terms of official documentation, per the Programming FAQ:
Remember that arguments are passed by assignment in Python.
The actual parameters (arguments) to a function call are introduced in the local symbol table of the called function when it is called; thus, arguments are passed using call by value (where the value is always an object reference, not the value of the object).
where the footnote adds:
Actually, call by object reference would be a better description, since if a mutable object is passed, the caller will see any changes the callee makes to it (items inserted into a list).
This is consistent with the rest of Python's assignment model, for example:
def somefunc(y): y.append(1) x =  somefunc(x) print x
is similar to:
x =  y = x y.append(1) print x
in that the object assigned to the name
x is also assigned to the name
y (albeit only within
somefunc in the former).
You can just ask Python herself:
def is_python_pass_by_value(foo): foo.append('More precisely, for reference types, it is call-by-object-sharing.') foo = ['Python is pass-by-reference.'] quux = ['Yes, of course, Python *is* pass-by-value!'] is_python_pass_by_value(quux) print(quux) # ['Yes, of course, Python *is* pass-by-value!', 'More precisely, for reference types, it is call-by-object-sharing.']
Call-by-sharing is simply a special case of pass-by-value, where the value being passed is an implicit pointer to shared (not necessarily mutable) state.
The semantics of passing and assigning are exactly the same in Python as in Java. Java is described on StackExchange and elsewhere on the Internet (as well as by you in your question) as pass-by-value ony, and these terms must be used consistently across languages to be meaningful; therefore, Python is pass-by-value only.
Forget call-by-whathaveyou for Python
Pythons scoping and parameter passing are quite simple if you do not try to understand them by these semi-well-defined call-by-X terms.
- A formal parameter (of a function) is a name.
- What you pass as the argument to the function represents an object.
- In the body of the function, that object will be bound to that name during function execution.
- Some objects are mutable (e.g. lists, class objects), others are not (e.g. strings, integers).
That's all there is.
Binding an object to a name (or a name to an object; it's the same)
is exactly what assignment does:
a = 1 will bind the object
1 to the name
a = b will bind the object-that-the-name-b-is-currently-bound-to to the name
def myfunc(a), the call
myfunc(b) will bind the name
a just like
a = b would, except that the object that
b refers to is also brought
to the right scope: the body of
See the Python language reference Section 4.1 Naming and Binding.