Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it Pythonic to use bools as ints?

Tags:

python

boolean

False is equivalent to 0 and True is equivalent 1 so it's possible to do something like this:

def bool_to_str(value):     """value should be a bool"""     return ['No', 'Yes'][value]  bool_to_str(True) 

Notice how value is bool but is used as an int.

Is this this kind of use Pythonic or should it be avoided?

like image 967
hwiechers Avatar asked Jul 04 '10 10:07

hwiechers


People also ask

Are bools ints in C++?

C++ does not really have a boolean type; bool is the same as int. Whenever an integer value is tested to see whether it is true of false, 0 is considered to be false and all other integers are considered be true.

Can bool be converted to int?

bool values are convertible to int type, with true converting to 1 and false converting to 0 .

Should I use bool or int?

If you choose a bool (boolean) type, it is clear there are only two acceptable values: true or false . If you use an int (integer) type, it is no longer clear that the intent of that variable can only be 1 or 0 or whatever values you chose to mean true and false .

Can boolean be converted to int in Python?

Use int for casting a Boolean value in Python. Using int() method will convert Boolean to Int, 1 for True and 0 for False.


1 Answers

I'll be the odd voice out (since all answers are decrying the use of the fact that False == 0 and True == 1, as the language guarantees) as I claim that the use of this fact to simplify your code is perfectly fine.

Historically, logical true/false operations tended to simply use 0 for false and 1 for true; in the course of Python 2.2's life-cycle, Guido noticed that too many modules started with assignments such as false = 0; true = 1 and this produced boilerplate and useless variation (the latter because the capitalization of true and false was all over the place -- some used all-caps, some all-lowercase, some cap-initial) and so introduced the bool subclass of int and its True and False constants.

There was quite some pushback at the time since many of us feared that the new type and constants would be used by Python newbies to restrict the language's abilities, but Guido was adamant that we were just being pessimistic: nobody would ever understand Python so badly, for example, as to avoid the perfectly natural use of False and True as list indices, or in a summation, or other such perfectly clear and useful idioms.

The answers to this thread prove we were right: as we feared, a total misunderstanding of the roles of this type and constants has emerged, and people are avoiding, and, worse!, urging others to avoid, perfectly natural Python constructs in favor of useless gyrations.

Fighting against the tide of such misunderstanding, I urge everybody to use Python as Python, not trying to force it into the mold of other languages whose functionality and preferred style are quite different. In Python, True and False are 99.9% like 1 and 0, differing exclusively in their str(...) (and thereby repr(...)) form -- for every other operation except stringification, just feel free to use them without contortions. That goes for indexing, arithmetic, bit operations, etc, etc, etc.

like image 60
Alex Martelli Avatar answered Sep 22 '22 17:09

Alex Martelli