Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way of distinguishing bools from numbers in Python?

Tags:

python

I have an application where I need to be able to distinguish between numbers and bools as quickly as possible. What are the alternatives apart from running isinstance(value, bool) first?

Edit: Thanks for the suggestions. Actually, what I want to be able to do have a check for numbers that leaves out bools so that I can reorder my checks (numbers are far more prevalent) and improve my negacalls. isinstance() itself is fast enough. The x is True or x is False is intriguing.

like image 619
Charlie Clark Avatar asked Feb 24 '15 10:02

Charlie Clark


People also ask

What is the Boolean type bool in Python?

In Python, the Boolean type bool is a subclass of int and can take the values True or False: As you can see in this code, Python implements bool as a subclass of int with two possible values, True and False. These values are built-in constants in Python. They’re internally implemented as integer numbers with the value 1 for True and 0 for False.

How to check if a boolean is true or false in Python?

Only an empty string is False, the rest of the strings are considered to be True. Even the string with space is True, as space is also a character in Python. 3. Lists, tuples, and dictionaries: These will be boolean False if they do not have any element or they are empty. Else, these are False. 4. Others Let us check the boolean for the others too.

How do you subtract two boolean values in Python?

Two or more boolean values can be subtracted using the ‘-’ operator. The True value is the same as 1 and the False is 0. 3. Multiplication: Two or more boolean values can be subtracted using the ‘-’ operator. The True value is the same as 1 and the False is 0. 4. Division and Integer division

What are the 5 most important things to know about pythons?

Python Booleans 1 Boolean Values. In programming you often need to know if an expression is True or False. ... 2 Evaluate Values and Variables 3 Most Values are True. Almost any value is evaluated to True if it has some sort of content. ... 4 Some Values are False. ... 5 Functions can Return a Boolean. ...


1 Answers

So, Padraic Cunningham suggests, that the following might be a bit faster. My own quick experiments with cProfile-ing it haven't shown any difference:

isbool = value is True or value is False

I assume that's as fast as you can get: Two non-type-coercing comparisons.

Edit: I replayed the timing tests from @user 5061 and added my statement. This is my result:

>>> import timeit
>>> stmt1 = "isinstance(123, bool)"
>>> stmt2 = "123 is True or 123 is False"
>>> t1 = timeit.timeit(stmt1)
>>> t2 = timeit.timeit(stmt2)
>>> print t1
0.172112941742
>>> print t2
0.0690350532532

Edit 2: Note, that I'm using Python 2.7 here. @user 5061 might use Python 3 (telling from the print() function), so any solution provided here should be tested by OP before putting in production, for YMMV.

like image 122
Boldewyn Avatar answered Oct 15 '22 02:10

Boldewyn