The Python Oracle

Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping

--

Chapters
00:00 Question
01:13 Accepted answer (Score 228)
03:17 Answer 2 (Score 85)
03:42 Answer 3 (Score 23)
04:22 Thank you

--

Full question
https://stackoverflow.com/questions/2764...

Accepted answer links:
[__index__]: http://docs.python.org/reference/datamod...
[mark-dickinson]: https://stackoverflow.com/users/270986/m...
[docs for python 2]: https://docs.python.org/2/reference/data...
[docs for Python 3]: https://docs.python.org/3/reference/data...
[for Python 2]: https://docs.python.org/2/library/stdtyp...

Answer 2 links:
http://www.python.org/dev/peps/pep-0285/

Answer 3 links:
[reserved words]: http://docs.python.org/release/3.0.1/wha...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #boolean #equality #languagespecifications

#avk47



ACCEPTED ANSWER

Score 240


In Python 2.x this is not guaranteed as it is possible for True and False to be reassigned. However, even if this happens, boolean True and boolean False are still properly returned for comparisons.

In Python 3.x True and False are keywords and will always be equal to 1 and 0.

Under normal circumstances in Python 2, and always in Python 3:

False object is of type bool which is a subclass of int:

    object
       |
     int
       |
     bool

It is the only reason why in your example, ['zero', 'one'][False] does work. It would not work with an object which is not a subclass of integer, because list indexing only works with integers, or objects that define a __index__ method (thanks mark-dickinson).

Edit:

It is true of the current python version, and of that of Python 3. The docs for python 2 and the docs for Python 3 both say:

There are two types of integers: [...] Integers (int) [...] Booleans (bool)

and in the boolean subsection:

Booleans: These represent the truth values False and True [...] Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

There is also, for Python 2:

In numeric contexts (for example when used as the argument to an arithmetic operator), they [False and True] behave like the integers 0 and 1, respectively.

So booleans are explicitly considered as integers in Python 2 and 3.

So you're safe until Python 4 comes along. ;-)




ANSWER 2

Score 88


Here's the PEP discussing the new bool type in Python 2.3: http://www.python.org/dev/peps/pep-0285/.

When converting a bool to an int, the integer value is always 0 or 1, but when converting an int to a bool, the boolean value is True for all integers except 0.

>>> int(False)
0
>>> int(True)
1
>>> bool(5)
True
>>> bool(-5)
True
>>> bool(0)
False



ANSWER 3

Score 24


In Python 2.x, it is not guaranteed at all:

>>> False = 5
>>> 0 == False
False

So it could change. In Python 3.x, True, False, and None are reserved words, so the above code would not work.

In general, with booleans you should assume that while False will always have an integer value of 0 (so long as you don't change it, as above), True could have any other value. I wouldn't necessarily rely on any guarantee that True==1, but on Python 3.x, this will always be the case, no matter what.




ANSWER 4

Score 4


Let's break this question into two parts.

Python:

print(45 == "45")  # Output - False

Javascript:

console.log(45 == "45")  # Output - true

Here, most people think Javascript only checks the value of both side object, But that's not true writing console.log(45 == "45") is similar to print(45 == "45").
But now you have a question if both syntax are similar why do I get different results?

In Javascript :

  • The == operator performs type coercion before comparing the values. This means that JavaScript will attempt to convert the operands to a common type before evaluating the equality.
  • In the case of 45 == "45" in JavaScript, the string "45" is automatically converted to a number because the other operand is a number. JavaScript tries to perform implicit type conversion to make the comparison. The string "45" can be converted to the number 45, and hence the comparison 45 == "45" evaluates to true.
  • Similar to Python keyword is, Javascript has a === Comparison Operator. Which checks both of side object is located in the same computer RAM memory location or not.

In Python :

  • In the Python, 45 and "45" have different types. 45 is an instance of class <integer>, and "45" is an instance of class <string>. They are not instances of the same class.
  • Unlike Javascript, Python doesn't do internal type conversion, So print(45 == "45") evaluates False.
  • Python used internal type conversion, Yes you heard right but here is one condition. Python will use internal type conversion only with boolean type objects.
  • So, in the expression print(True == 1), the boolean value True is implicitly coerced to the integer 1 before performing the comparison. As a result, the comparison print(1 == 1) is evaluated, which is True.
  • When you write print(True == 1) is equivalent to print(1 == 1). When Python interpreter reaches this code of line before executing this code it is convert print(True == 1) to print(int(True) == 1), So finally it becomes print(1 == 1) and now according to == rule both side object value and type <class int> are same hence you see True output in terminal.
  • Similarly, print(False == 0) is equivalent to print(int(False) == 0) and finally print(0 == 0) evaluate to True.

Check HERE.

# Normal behavior
sample_1 = True
print(sample_1) # Output - True
print(int(sample_1)) # Output - 1

sample_2 = False
print(sample_2) # Output - False
print(int(sample_2)) # Output - 0


# Try with True - boolean
A = True + 5  # equivalent to `A = int(True) + 5`
print(A) # Output - 6 (True ---> 1)


# Try with False - boolean
B = False + 5 # equivalent to `A = int(False) + 5`
print(B) # Output - 5 (False ---> 0)


# Try both boolean together
final = False + True # equivalent to `final = int(False) + int(True)`
print(final) # Output - 1  (False ---> 0 & True ---> 1)

Hope it helps everyone!