7

Many people say that in Python arguments to functions are passed using a call-by-value model. As I understand it, it is not actually a call-by-value language, but a call-by-object or call-by-sharing model.

What are the differences between a call-by-value model and a call-by-object model? What is an example in Python that shows how these models are different?

5
  • It's "pass-by-reference" or "pass-by-value"
    – jdi
    Jun 1, 2012 at 3:47
  • Also "call-by-reference" and "call-by-value".
    – cytinus
    Jun 1, 2012 at 3:49
  • I prefer to think of it as "call-by-value-of-the-reference" Jun 1, 2012 at 3:59
  • @gnibbler - you mean referring specifically to Python?
    – cytinus
    Jun 1, 2012 at 4:09
  • @cytinus, Yes, although it may apply to other languages too. Jun 1, 2012 at 4:44

2 Answers 2

12

Variables in Python aren't values, they're object references. When you call a Python function the arguments are copies of the references to the original object. I don't know how this relates to the terminology you posed in the question.

For example consider the following Python code:

def foo(bar, baz):
    bar = 3
    baz[0] = 4

a = 1
b = [2]
foo(a, b)
print a, b

a is assigned to the object 1, and b is assigned to the list object containing a reference to the object 2. Inside of the function foo, bar is also assigned to the same object 1 and baz is assigned to the same list object. Since 1 is immutable you can't change the object, but you can reassign bar to refer to a different object such as 3. A list is modifiable, so by setting baz[0] to 4 you are also changing the list object that b refers to. The output of the above will be 1 [4].

1
  • Brilliant explanation. Specially the object references concept. Mar 31, 2016 at 6:35
8

Saying that it is not pass-by-value is not correct. Semantically, it is pass-by-value, and one can demonstrate the semantic equivalence between it and other pass-by-value languages. However, it belongs to a particular subcategory of pass-by-value languages where all the values are references (pointers) to objects (you cannot have an object as a value directly), and many people confuse a pointer to an object with the object itself. Such languages include Java (with respect to objects), Python, Ruby, JavaScript, Scheme, Smalltalk, just to name a few.

To add to the confusion is that the communities of the different languages use different terminology. For example, the Java community consistently describes Java as pass-by-value, even though objects in Java have the exact same semantics as objects in Python and Ruby. However, you hear "pass-by-value" much less in these latter languages' communities.

Pass-by-value vs. pass-by-reference is essentially a semantic distinction, not one of what it is used for. But many (beginner) programmers don't care so much about semantics, and care more about what it can be used to do. For example, some people think that whenever you have "I have some data that I give to a function and it can change it without returning it, and I can see the changes", that means pass-by-reference, without actually thinking (or caring) about whether the thing that was changed was the thing you passed, or something that was indirectly pointed to by the thing you passed.

"Pass by object" or "pass by sharing" or whatever is a term that coined to describe the combination of pass-by-value semantics along with having all values be references (pointers). In this way, they can more easily express the actual effects of such a combination, and distinguish it from the effects of pass-by-value in C, for example. But semantically it still is pass-by-value. Saying that variables are "names" or "handles" that are "bound" to objects and that when you assign or pass it, you share the object, is exactly equivalent to saying that variables (and in fact, all values in the language) are pointers to objects, and when you assign or pass them, these pointers are copied by value.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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