views:

196

answers:

4

I sent a reference to a bool object, and I modified it within a method. After the method finished it's execution, the value of the bool outside the method was unchanged.

This leads me to believe that Python's bools are passed by value. Is that true? What other Python types behave that way?

+1  A: 

It depends on whether the object is mutable or immutable. Immutable objects behave like you saw with the bool, whereas mutable objects will change.

For reference: http://www.testingreflections.com/node/view/5126

Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference... what's the deal?

It is important to understand mutable and immutable objects. Some objects, like strings, tuples, and numbers, are immutable. Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed. Other objects, like lists and dictionaries are mutable, which means you can change the object in-place. Therefore, altering an object inside a function/method will also change the original object outside.

FModa3
-1: Firstly this seems to be a copy of the accepted answer to the duplicate question mentioned (which could be coincidental). Secondly you just can't alter immutable objects - that's what immutable means. Saying that altering immutable objects creates a new instance is just confusing the issue. You can rebind the name of an immutable object, but that's not the same thing!
Scott Griffiths
Interesting, I didn't even see the duplicate. This question wasn't labeled duplicate when I answered. Googling "Python pass by reference" gave me the link above. Also, coincidently, the quote you have issue with is the accepted answer by the duplicate. I don't find it difficult to understand that if Python can edit in place it will, and if it can't it will make a local copy. Here is another site who cite's the link above but provides more explanation: http://bogdan.org.ua/2008/02/11/python-passing-by-value-vs-passing-by-reference.html
FModa3
+8  A: 

Python variables are not "references" in the C++ sense. Rather, they are simply local names bound to an object at some arbitrary location in memory. If that object is itself mutable, changes to it will be visible in other scopes that have bound a name to the object. Many primitive types (including bool, int, str, and tuple) are immutable however. You cannot change their value in-place; rather, you assign a new value to the same name in your local scope.

In fact, almost any time* you see code of the form foo = X, it means that the name foo is being assigned a new value (X) within your current local namespace, not that a location in memory named by foo is having its internal pointer updated to refer instead to the location of X.

*- the only exception to this in Python is setter methods for properties, which may allow you to write obj.foo = X and have it rewritten in the background to instead call a method like obj.setFoo(X).

rcoder
Yes, strictly speaking EVERYTHING in Python is passed by address. Python only has one arg-passing process.It's just the fact that some objects are mutable and others are not gives the illusion that something magically more complex is occurring.
Salim Fadhley
+1  A: 

In short, there are no variables in Python; there are objects (Like True and False, the bools happen to be immutable), and names. Names are what you call variables, but names belong to a scope, you can't normally change names other than the local names.

kaizer.se
+1  A: 

The thing to remember is that there is no way in Python for a function or a method to rebind a name in the calling namespace. When you write "I sent a reference to a bool object, and I modified it within a method", what you actually did (I am guessing) was to rebind the parameter name (to which the bool value was bound by the call) inside the method body.