Neither.
If you twist around how you interpret the terms, you can call it either. Java calls the same evaluation strategy "pass by value", while Ruby calls it "pass by reference". But the Python documentation carefully avoids using either term, which is smart.
(I've been told that Jeff Knupp has a similar blog post called Is Python call-by-value or call-by-reference? Neither. His seems like it might be more accessible than mine, and it covers most of the same information, so maybe go read that if you get confused here, or maybe even read his first and then just skim mine.)
Variables and values
The distinction between "pass by value" and "pass by reference" is all about variables. Which is why it doesn't apply to Python.In, say, C++, a variable is a typed memory slot. Values live in these slots. If you want to put a value in two different variables, you have to make a copy of the value. Meanwhile, one of the types that variables can have is "reference to a variable of type Foo". Pass by value means copying the value from the argument variable into the parameter variable. Pass by reference means creating a new reference-to-variable value that refers to the argument variable, and putting that in the parameter variable.
In Python, values live somewhere on their own, and have their own types, and variables are just names for those values. There is no copying, and there are no references to variables. If you want to give a value two names, that's no problem. And when you call a function, that's all that happens—the parameter becomes another name for the same value. It's not the same as pass by value, because you're not copying values. It's not the same as pass by reference, because you're not making references to variables. There are incomplete analogies with both, but really, it's a different thing from either.
A simple example
Consider this Python code:def spam(eggs): eggs.append(1) eggs = [2, 3] ham = [0] spam(ham) print(ham)
If Python passed by value, eggs would have a copy of the value [0]. No matter what it did to that copy, the original value, in ham, would remain untouched. So, it would print out [0].
If Python passed by reference, eggs would be a reference to the variable ham. It could mutate ham's value, and even put a different value in it. So, it would print out [2, 3].
But in fact, eggs becomes a new name for the same value [0] that ham is a name for. When it mutates that value, ham sees the change. When it rebinds eggs to a different value, ham is still naming the original value. So, it prints out [0, 1].
Ways to confuse yourself
People learn early in their schooling that pass by value and pass by reference are the two options, and when they learn Python, they try to figure out which one of the two it does. If you think about assignment, but not about mutability, Python looks like pass by value. If you think about mutability, but not assignment, Python looks like pass by reference. But it's neither.Why does Java call this "pass by value"?
In Java, there are actually two types of values—native values, like integers, and class values. With native values, when you write "int i = 1", you're creating an int-typed memory slot and copying the number 1 into it. But with class values, when you type Foo foo = new Foo()", you're creating a new Foo somewhere, and also creating a reference-to-Foo-typed memory slot and copying a reference to that new Foo into it. When you call a function that takes an int argument, Java copies the int value from your variable to the parameter. And when you call a function that takes a Foo argument, Java copies the reference-to-Foo value from your variable to the parameter. So, in that sense, Java is calling by value. (This is basically the same thing that most Python implementations actually do deep under the covers, but you don't normally have to, or want to, think about it. Java puts that right up on the surface.)Why does Ruby call this "pass by reference"?
Basically, Ruby is emphasizing the fact that you can modify things by passing them as arguments.This makes more sense for Ruby than for Python, because Ruby is chock full of mutating methods, and there's an in-place way to do almost everything, while Python only has in-place methods for the handful of things where it really matters. For example, in Python, there is no in-place equivalent of filter, but in Ruby, select! is the in-place equivalent of select.
But really, it's still sloppy terminology in Ruby. If you assign to a parameter inside a function (or block), it does not affect the argument any more than in Python.
So what should we call it?
People have tried to come up with good names. Before Python ever existed, Barbara Liskov realized that there was an entirely different evaluations strategy from pass by value and pass by reference, and called it "pass by object". Others have called it "pass by sharing". Or, just to confuse novices even farther, "pass by value reference". But none of these names ever gained traction; nobody is going to know what these names mean if you use them.
So, just don't call it anything. Anyone who tries to answer a question by saying, "Well, Python passes by reference [or by value, or by some obscure term no one has ever heard of], so…" is just confusing things, not explaining.
When Python's parameter evaluation strategy actually matters, you have to describe how it works and how it matters. So just do that.
When it doesn't matter, don't bring it up.
Ways to un-confuse yourself
Instead of trying to get your head around how argument passing works in Python, get your head around how variables work. That's where the big difference from languages like Java and C++ lies, and once you understand that difference, argument passing becomes simple and obvious.How do I do real pass by reference, then?
You don't. If you want mutable objects, use mutable objects. If you want a function to re-bind a variable given to it as an argument, you're almost always making the same mistake as when you try to dynamically create variables by name.
And that should be a clue to one way you can get around it when "almost always" isn't "always": Just pass the name, as a string. (And, if it's not implicitly obvious what namespace to look the name up in, pass that information as well.)
But usually, it's both simpler and clearer to wrap your state up in some kind of mutable object—a list, a dict, an instance of some class, even a generic namespace object—and pass that around and mutate it.
What about closures?
Yes, using closures can also be a way around needing to pass by reference. This shouldn't be too surprising—closures and objects are dual concepts in a lot of ways.
I meant how do closures work, if there's no pass by reference?
Oh, you just had to ask, didn't you. :)
The simple, but inaccurate, way to think about it is that a closure stores a name as a string and a scope to look it up in. Just like I just told you not to do. But it's happening under the covers. In particular, your Python code never uses the variable name as a string. The fact that the interpreter uses the variable name as a string doesn't matter; it always uses names as strings. If you look at the attributes of a function object and its code object, you'll see the names of any globals you reference (including modules, top-level functions, etc.) any locals you create, the function parameters themselves, etc.
But if you look carefully (or think about it hard enough), you'll realize that this isn't actually sufficient for closures. Closures are implemented by storing special cell objects in the function object, which are basically references to variables in some nonlocal frame, and there are special bytecodes to load and store those variables. (The code that owns the frame uses those bytecodes to access the variables it exposes to the closure; the code inside the closure uses them to access the variables it has cells for.) You can even see one of these cell objects in Python, and read the value of the referenced variable (func.__closure__[0].cell_contents, it you're dying of curiosity), although you can't write the value this way.
And if you're now thinking, "Aha, with some bytecode hacking, I could fake call by reference". Well, yeah, but you think it'll be simpler than passing around a 1-element list so you can fake call by reference?
View comments