DATA SCIENCE

In Data Science and programming, boolean (True and False) data are considered simple and boring. I believe booleans are fun and sometimes unpredictable if you don’t know their logic. I will convince many of you that some interesting features are associated with boolean objects that you probably ignored.
Easy Puzzle
Let me start with an easy one.
This code should be able to recognize int and boolean objects. If the input object is not boolean or integer, it tells you that the object is "Something Else!". In the end, I tested the function with four examples. What is your guess about the outputs of this piece of code? It seems easy, right?
An int object.
Something else!
A bool object.
A bool object.
If your guess was like the above outputs, you are as WRONG as I was for the first time. Actually, the right answer is:
An int object.
Something else!
An int object.
An int object.
Why are the boolean objects (Ture and False) identified as integer objects?
In fact, in Python, the boolean class is inherited from the integer class. To make a long story short, there was no boolean object in initial versions of Python, and they used 1 and 0 instead of True and False. When they decided to incorporate boolean objects in Python, they inherited the boolean class from the integer class (read this story here).
When you run the code, because the code recognized True and False as types of int, it prints "An int object." and makes you confused and surprised.
Hard Puzzle
Okay, this one was easy. Let me show you another piece of code and ask you to guess the outputs.
The first time I faced this challenge, it seemed super easy. I thought, first of all, both functions are almost the same and should give me the same results. The 0 is False, 1 is True, and 2 should be False. Simply, I expected something like:
False
True
False
False
True
False
But, as you guess, my answer was not correct. The correct answer is:
False
True
True
False
True
False
What?! It means if 2
and if 2 == True
are not equal in Python, seriously? The first expression is True, and the second one is False. How is it possible?!
Let me tell you why does it happen. We have two if
expressions. Let’s start with the easy one, which is if 2 == True
. As you know, in Python, True
is equal to 1
. Therefore 2 == True
is simply False, and that’s why the code block associated with if 2 == True
does not get executed. It is easy to understand.
The mystery is why the code block corresponding to if 2
(in func1()
) gets executed and returns True? Does it mean that 2
is equal to True
? If 2
is equal to True
, why 2 == True
is not True. Confusing, right?
If you are curious to know the answer, here is it. The answer is in object-oriented Programming which is the foundation of Python. As you know, everything in Python is an object. Objects have attributes and methods. Some attributes and methods are called magic methods or attributes.
Although it is not necessary for the rest of this article, I encourage you to read my trilogy on object-oriented programming in Python.
Magic bool
One of the magic methods that you can define for every class and object is __bool__
. This magic method can return either False
or True
. When you use if and an object (e.g., if obj:
), if
calls __bool__
method and based on its returned value, execute or does not execute the corresponding block. Let’s do a quick test in Python and check what does __bool__
method returns for integer object 2
and 0
.
>>> n = 2
>>> n.__bool__()
True
>>> n = 0
>>> n.__bool__()
False
You can also check the boolean value of an object using a built-in faction called bool()
.
>>> n = 2
>>> bool(n)
True
You can take advantage of __bool__
to improve object-oriented programming. Think what is the most important boolean aspect of your object, and based on that, return a proper boolean value through __bool__
method. For example, suppose you are working on a reinforcement learning project and have an object for your robot or agent. The most important aspect of a robot/agent, in my opinion, is if it is alive or not. Therefore, I define the __bool__
method to reflect this property in its returned value. Here is an example of how I will define this class of objects:
In this example, there is an attribute called score
, which stores the robot object’s score. While the score is above 0, your robot is alive and bool()
returns True. We reduce the robot score by 10 and then 90 (the initial score was 100). As soon as the score becomes 0, the object returns False in response to any functions that call its boolean value. The user can take advantage of this property in her/his code, for example, while r: ...
or if r: ...
.
If there is no __bool__
method for the object, if
, bool()
and similar functions look for another magic method which is called __len__
. This method normally returns the length of the object. If __len__
return 0, bool()
returns False; otherwise, it returns True. In the previous example, simply we could return the score through __len__
, and we did not need to define __bool__
anymore. Here is how we can change the code and get the same results.
With all knowledge that you learned, you must be able to answer my last puzzle. What is the output of the following code?
bool("")
Summary
To understand the behavior of boolean data types and objects in Python, you must know that this data type is inherited from the integer class. Also, magic methods such as __bool__
and __len__
define how an object response to functions like bool(obj)
.
Follow me on Twitter for the latest stories: https://twitter.com/TamimiNas