Are Python's bools passed by value?
Asked Answered
D

5

10

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?

Dufresne answered 29/9, 2009 at 20:17 Comment(1)
Duplicate, please see https://mcmap.net/q/37849/-passing-values-in-python-duplicateTalipes
B
13

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

Bleareyed answered 29/9, 2009 at 20:23 Comment(1)
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.Diaconicum
G
2
  • True and False are 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.

enter image description here

Because they are singleton objects, they will always retain their same memory address throughout the lifetime of your program.

  • Python has bool class that used to represent Boolean values. However, the bool class is a subclass of the int class. Since bool objects are also int objects, they can also be interpreted as the integers 1 and 0

    int(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.

Galitea answered 20/3, 2022 at 5:14 Comment(0)
C
1

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.

Cyzicus answered 29/9, 2009 at 20:22 Comment(2)
-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!Roxyroy
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: bogdan.org.ua/2008/02/11/…Cyzicus
S
1

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.

Sybyl answered 30/9, 2009 at 2:51 Comment(0)
E
0

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.

Eunuchize answered 29/9, 2009 at 20:24 Comment(1)
"There are no variables in Python" is pedantic and misleading, I really wish people would stop saying it.Defy

© 2022 - 2024 — McMap. All rights reserved.