9

Possible Duplicate:
Is False == 0 and True == 1 in Python an implementation detail or is it guaranteed by the language?

I noticed today that the following works using python 2.6 (Cpython)...

>>> a=[100,200]
>>> a[True]
200
>>> a[False]
100

Is this portable to other python implementations (e.g. is True/False guaranteed to inherit from int? Is True guaranteed to evaluate to 1 instead of some other non-zero number?) Is there any situation where this would be useful? It seems like it could be used as another form of a ternary operator, but I don't know how much is gained there...

Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • 6
    (Not an answer, but you may find it interesting: In Python 2.6 and before True and False are just variables so ... `True, False = "hello", "world"` ... but they have proper *reserved* status in Python 3.x) –  Apr 21 '12 at 23:31
  • As far as I know, True and False are labels for 1 and 0. 1==True and 0==False. Therefore, it would return the 0th and 1st items in the list – Gwyn Howell Apr 21 '12 at 23:32
  • @gwynhowell: no, `True` and `False` are objects in their own right, of type `bool`. – Fred Foo Apr 21 '12 at 23:34
  • @gwynhowell Yes, that is what I show (as far as returning the 0 and 1 item in the list), but I wonder if it is guaranteed by the standard, or an implementation detail... Also note that True and False are not exactly the same as 1 and 0 (`True is 1` -> `False`, `False is 0` -> `False`) – mgilson Apr 21 '12 at 23:34
  • @larsmans Yes, this is a duplicate of that question. I didn't find that in my searching. Thanks. – mgilson Apr 21 '12 at 23:43
  • In 2.6 and 2.7, they are `int`s. I sometimes used to use them to pick the correct execution. Eg: eval(["y/x", "x/y"][x>y]). But using a ternary operator is better in almost all cases. – inspectorG4dget Apr 22 '12 at 00:40

2 Answers2

11

It is part of the language specification, so any Python implementation should implement the booleans as equivalent to the integers.

Booleans

These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of plain integers, and 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.

Community
  • 1
  • 1
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
4

Yes -- this is guaranteed -- with the caveat that True and False may be reassigned; but that doesn't affect the results of boolean operations. (Thanks to Ignacio for the documentary proof.) In fact, back when there was no ternary operator, this was one of the methods used to emulate it. Nowadays, if you want a ternary operator, use the ternary operator. But sometimes this construct is still useful. For example:

>>> even_odd = [[], []]
>>> for i in range(10):
...     even_odd[i % 2 == 1].append(i)
... 
>>> print even_odd
[[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]

You can do this with a dictionary as well. It has a ternary operator equivalent...

>>> even, odd = [], []
>>> for i in range(10):
...     (even if i % 2 == 1 else odd).append(i)
... 
>>> even, odd
([1, 3, 5, 7, 9], [0, 2, 4, 6, 8])

But I actually find the list-indexing version easier to read, at least in this case. YYMV.

Community
  • 1
  • 1
senderle
  • 145,869
  • 36
  • 209
  • 233