Python Calling Conventions – Argument Passing Mechanism and Official Documentation

calling-conventionspython

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.

Best Answer

In terms of official documentation, per the Programming FAQ:

Remember that arguments are passed by assignment in Python.

Elsewhere in the docs:

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 = [0]
somefunc(x)
print x

is similar to:

x = [0]
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).