'Are Python's bools passed by value?
I sent a reference to a bool object, and I modified it within a method. After the method finished its 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?
Solution 1:[1]
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).
Solution 2:[2]
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.
Solution 3:[3]
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.
Solution 4:[4]
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.
Solution 5:[5]
TrueandFalseare singleton objects.
True is nothing more than a label that points to some object in memory that is a bool type, lives at some memory address and it has internal value of integer 1. False is the same thing which has an interval value of 0.
Because they are singleton objects, they will always retain their same memory address throughout the lifetime of your program.
Python has
boolclass that used to represent Boolean values. However, the bool class is a subclass of the int class. Sinceboolobjects are alsointobjects, they can also be interpreted as the integers 1 and 0int(True) -> 1 int(False) ->0
IMPORTANT to understand, True and 1 are not same objects. True lives at some memory address and ` lives at some different memory address. we compare their references here:
id(True) != id(1)
Or
True is 1 -> False
Because identity operator checks the memory address
Passing values
True > False --> True
This is strange behavior. In this case, since True holds 1 and False holds 0
1 > 0
another example
a=True+True+True
a will be 3. it seems right we are passing values but everything is passed by reference - it's just that for singleton objects it's the same reference.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rcoder |
| Solution 2 | Community |
| Solution 3 | holdenweb |
| Solution 4 | u0b34a0f6ae |
| Solution 5 |

