4

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.

6

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).

11

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.

0

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.

  • Cross-language consistency is nice, but why should the Python folks use the Java terminology rather than the other way around? There are good arguments to discard the value/reference binary. Moreover, in Java there are primitive types which are passed by value in the most traditional sense, while in Python there aren't. – user7043 Dec 1 '14 at 12:20
  • @delnan: I didn't say everything in Java is in Python. Types are not relevant, because Java is pass-by-value regardless of type. The semantics don't care about type. The same is true in Python. It's not "the Java terminology". It's the same terminology that is used in C, C++, and other languages (including Python, Ruby, etc.) by folks who have thought about it. The OP mentioned Java is pass-by-value, so it was a good place to compare to. The reasoning is independent of language. – user102008 Dec 1 '14 at 19:21
  • @delnan: The only definition of pass-by-value/pass-by-reference that I have seen that is consistent across languages, and only relies on semantics and not vague terms, is the one where C/Java/Python/Ruby are pass-by-value only. No other definition I've seen commonly is used in a semantically consistent way. – user102008 Dec 1 '14 at 19:26
  • Let me put it that way: In Python, "value" is an overly broad term. What's actually being passed is always the value of a reference (so it's kind of a special case), and "value" is commonly used to refer to an object's internal state (which is never passed). For these and other reasons, people argue that "pass by value", while technically accurate, is not the best wording to describe Python's behavior. The notion that argument passing is either by value or by reference dates back to a very early age of computing, where programming languages semantics were somewhat different. – user7043 Dec 1 '14 at 19:34
  • @delnan: In any language, "value" is an overly broad term. In C, when the parameter is a pointer type, is the "value" the address, or the thing pointed to? Same in Java. But "pass-by-value" and "pass-by-reference" should not depend on vague notions of terms like "value" -- Here is a definition only on semantics: when you assign to a parameter inside the function, and it has the same effect as assigning to the passed variable in the calling scope, that is pass-by-reference; when it has no effect on the calling scope, that is pass-by-value. This is consistent with how it is used in C++ and Java. – user102008 Dec 1 '14 at 20:03
0

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 and a = b will bind the object-that-the-name-b-is-currently-bound-to to the name a.

After 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 myfunc.

See the Python language reference Section 4.1 Naming and Binding.

  • That's exactly what's called pass-by-value -- passing works the same way as assigning. – user102008 Feb 27 '15 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.