What is the practical application of bool() in Python?
--
Track title: CC F Haydns String Quartet No 53 in D
--
Chapters
00:00 Question
00:40 Accepted answer (Score 23)
01:51 Answer 2 (Score 6)
03:25 Answer 3 (Score 4)
04:31 Answer 4 (Score -1)
04:42 Thank you
--
Full question
https://stackoverflow.com/questions/2486...
Question links:
[this tutorial]: http://openbookproject.net/thinkcs/pytho.../
[here]: http://openbookproject.net/thinkcs/pytho...
Accepted answer links:
[truth value]: https://docs.python.org/3/library/stdtyp...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python27 #boolean
#avk47
ACCEPTED ANSWER
Score 23
It lets you convert any Python value to a boolean value.
Sometimes you want to store either True or False depending on another Python object. Instead of:
if python_object:
result = True
else:
result = False
you simply do:
result = bool(python_object)
How Python objects are converted to a boolean value, all depends on their truth value. Generally speaking, None, numeric 0 and empty containers (empty list, dictionary, set, tuple, string, etc.) are all False, the rest is True.
You use it whenever you need an explicit boolean value. Say you are building an object tree, and you want to include a method that returns True if there are children in the tree:
class Tree(object):
def __init__(self, children):
self.children = children
def has_children(self):
return bool(self.children)
Now Tree().has_children() will return True when self.children is not empty, False otherwise.
ANSWER 2
Score 6
To understand what bool() does we need to first understand the concept of a boolean.
A boolean variable is represented by either a 0 or 1 in binary in most programming languages. A 1 represents a "True" and a 0 represents a "False"
The number 1 is different from a boolean value of True in some respects. For example, take the following code:
>>> 1 is True
False
Notice that 1 is different than True according to Python. However:
>>> bool(1) is True
True
When we use the bool() function here, we convert 1 to a boolean. This conversion is called "casting". Casting 1 to boolean returns the value of "True".
Most objects can be cast to a boolean value. From my experience, you should expect every standard object to evaluate to True unless it is 0, None, False or an empty iterable (for example: "", [], or {}). So as an example:
>>> bool({})
False
>>> bool({"":False})
True
>>> bool(None)
False
>>> bool("")
False
>>> bool("hello")
True
>>> bool(500)
True
>>> bool(0)
False
>>> bool(False)
False
>>> bool(-1)
True
Lastly, a boolean prints as either "True" or "False"
>>> print bool(1)
True
ANSWER 3
Score 4
bool exposes the fact that Python allows for boolean conversions to things that you wouldn't typically consider to be True or False.
An example of this is lists. If len(my_list) would be greater than 0, it also treats this as True. If it has no length -- if len() would return 0 -- it is False. This lets you write code like this:
def check_list_for_values(my_list, value):
return [x for x in my_list if x == value]
your_list = [5, 6, 7, 8, 9, 5, 3, 4, 8]
if check_list_for_values(3, your_list):
print "got a match"
If check_list_for_values returns a list that has length greater than 0, then it prints "got a match" because it evaluates to True. If there is no length to the list that would be returned...
your_list = [5, 6, 7, 8, 9, 5, 3, 4, 8]
if check_list_for_values('elephant', your_list):
print "got a match"
Then there will be nothing printed, because it evaluates to False.