Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between bool() and operator.truth()?

bool() and operator.truth() both test whether a value is truthy or falsy and they seem rather similar from the docs, it even says in the truth() docs that:

This is equivalent to using the bool constructor.

However, truth() is over twice as fast as bool() from a simple test (Python 3.6 timings shown, but 2.7 is similar):

from timeit import timeit
print(timeit('bool(1)', number=10000000))
# 2.180289956042543
print(timeit('truth(1)', setup='from operator import truth', number=10000000))
# 0.7202018899843097

So what are the differences? Should I use truth() instead of bool()?

This Q&A arose after extensive comments and discussion with ShadowRanger under this question.

like image 296
Chris_Rands Avatar asked Feb 27 '18 13:02

Chris_Rands


People also ask

What is the difference between AND AND OR Boolean?

Both of them combine Boolean values ( true/false values ) into one Boolean value. But each does this in a different way: All the values AND combines must be true to get a true. At least one of the values OR combines must be true to get a true.

What are the difference between OR AND AND operators?

Both require two operands which may evaluate to true or false. The and operator returns True only if both operands are True. The or operator returns True if either operand is true.

What are the difference between AND AND OR operators give examples?

OR operator is also used to combine multiple conditions with WHERE clause. The only difference between AND and OR is their behaviour. When we use AND to combine two or more than two conditions, records satisfying all the specified conditions will be there in the result.

What is the difference between AND in Python?

and is a Logical AND that returns True if both the operands are true whereas '&' is a bitwise operator in Python that acts on bits and performs bit by bit operation. Note: When an integer value is 0, it is considered as False otherwise True when using logically.

What are Boolean operators?

Boolean Operators are the operators that operate on the Boolean values, and if it is applied on a non-Boolean value, then the value is first typecasted and then operated upon. These might also be regarded as the logical operators, and the final result of the Boolean operation is a Boolean value, True or False.

What is the truth table for the ‘and’ and ‘or’ operators?

The ‘and’ operator and the ‘or’ operator are the two binary Boolean operators that operate on some logic to give the Boolean value again. The standard Truth table for these two logical binary Boolean operators is as follows. The truth table for the ‘and’ operator.

How are strings compared with boolean operators in practice?

Here are how strings are compared with Boolean operators in practice: The output for the program above returns the following. The string "Sammy" above is not equal to the string "sammy", because they are not identical; one starts with an upper-case S and the other with a lower-case s.

How does Boolean logic work?

To get more technical, boolean logic is a way of representing how bits in a computer are processed. Let’s explore more about these conditional statements (e.g. if-else, where, or case-when statements) with truth tables to understand how precisely boolean logic works. This returns the value C, when the values A and B are true.


1 Answers

Although bool() and operator.truth() output the same result for the major uses cases their implementation is actually rather different. bool() is a class or type constructor while truth() is a narrow optimised regular function.

In practical terms, there are also two differences: 1) bool() called with no arguments returns False while truth() requires an argument. 2) bool() accepts an x key word argument, like bool(x=1), while truth() takes no keyword arguments. Both of these add overhead to bool() for the regular use cases.

The key word implementation is odd since likely no-one needs it and the name x is hardly descriptive. Issue29695 covers this, and in fact the issue impacts not just bool() but other classes like int() or list(). However, from Python 3.7 onwards these key word arguments will be removed, and speed should improve. Nonetheless, I tested the timings on the latest Python 3.8 branch, and bool() is faster than before but still over twice as slow as truth(), presumably due to the more generic implementation of bool().

So, if you have a task where speed is of high importance I would recommend using truth() over bool() if you require a function (for example to parse as a key to sorted()). However, as khelwood points out, bool() can still be faster occasionally, such as filter(bool, iterable), so it is probably best to time your use case to be certain of the best option.

Of course, if you don't need a function and simply want to test if a value is truthy or falsy you should use the idiomatic if or if not statements, which are fastest as khelwood and user2357112 commented.

This Q&A arose after extensive comments and discussion with ShadowRanger under this question.

like image 185
Chris_Rands Avatar answered Oct 24 '22 03:10

Chris_Rands