Python Boolean Testing
To talk about Python Boolean testing, let’s begin where Booleans are used most frequently, and this is in
if statements. So, consider this
if statement. We’re going to run the function
do_something() if some expression evaluates to
True. Now, the actual expression that’s in the
if statement may not necessarily be a Boolean value, but whatever some expression evaluates to, Python will determine whether that value is truthy—which would evaluate to the Boolean
True—or if it’s falsy—which would then evaluate to the Boolean
Now, any object is considered truthy unless its class defines a
.__bool__() method and the
.__bool__() method then specifies how or when the object should have a
False value, or it should specify the
.__len__() (length) method and when the
.__len__() method returns a zero value, then that evaluates to the Boolean
None value, which is the only instance of the
None object, is considered
False. Of course, the Boolean
False value is falsy. Zero of any numeric type—like the integer
0, the float
0.0, and so on—they’re all considered falsy.
So here’s a quick example. We’ve got this function called
message(). It takes in one parameter,
msg, initially set to
None, which means then that if
message() is called with no input, then
msg gets set a value of
None inside the function.
And if we check out the Boolean value of this function
func(), then we’re going to get
True. Now let’s do the same thing, say, for a class. This’ll be an
Article class and for the moment, let’s just pass on defining anything in the class.
Now let’s redefine the
Article class, and this time let’s define a property called
.published, and it’s going to start off with an initial value of
False. And let’s define the
.__bool__() method and so this will return the status, or the state, of the
.published property. So let’s return
Okay. And then, why don’t you go ahead and create a new instance of this
Article class? Say,
article, and now let’s check out the Boolean value of this
article instance. Okay. So in this case, because of course the value of the
.published property is
False, then we’re going to get a
False value. Now if you go ahead and change this property
.published to a value of
True, when you now compute the Boolean value of this
article instance, you’re going to get
True. Now, that means that if you were to use this
article instance in an
if statement—so if you were to write
if article—so this is basically “If the
article has been published, then we’re going to go ahead and do something with this
article.” So in this case, we can write something like
"The article has been published!" Okay.
So this is sort of a way for you to build into your class what it means for whatever instance you’re working with to have a truth value. And so in this case, we’re going to get
The article has been published! because the
.published property has a
True Boolean value. All right!
Go ahead and define a class that describes a network or a mathematical graph or a discreet graph. This class is going to have a list of vertices, and so we’re going to set that to an empty list initially, and then the
.__len__() method is going to return the number of vertices in the graph or the number of vertices in the network.
And so in this case, you’re going to return the length of the property that holds this list of vertices. Okay, so now let’s use this
Graph class. Let’s suppose that you’re using this class to model some sort of network of airports.
Now, currently the
airports instance of this
Graph class—the length of the
.vertices list is
0. So if we were to compute the Boolean value of this
airports instance, we’re going to get
False. And so here, Python is going in, computing the length of the
.vertices and in this case, it’s going to be
0, and so that evaluates to
False. All right, now let’s suppose you were to add some airports to your graph model, and so the vertices are going to be, let’s just say, JFK, and we’re going to add, say, LAX. And then why don’t you add, also, Miami.
All right, so now the
.vertices list has a nonzero length, and so if we compute the Boolean value of
airports, we’re going to get
True, right? The length of
.vertices is nonzero, that is what Python is doing to compute the Boolean value of
So, like you did with the
article instance, you could do the same thing for the airports model. And if the
airports instance did have airports in the
.vertices, then you would go ahead and do something with that. So if
True, meaning that the
airports instance did have some vertices or some airports, maybe you would go ahead and—let’s just try something simple—just print out what the vertices are. And in this case, we just simply get JFK, LAX, and Miami. However, if
airports.vertices, let’s say this was back to the empty list, Then if we run that exact same
if statement and print out the
airports, then because the list has a length of
0, it evaluates to
False. And then when we run that
if statement, nothing happens. We don’t get that print statement.
It’s going to be some class that keeps track of users on a website. It’s going to have a property of
.active, so whether the user is active or not, and then it’s going to keep track of the posts made by the user.
This is just going to be a list. And let’s define a length method. This will be the number of posts that the user has made on the website, so we’re going to return the length of the
And let’s maybe add some posts that the user has made, and so we’ll say something like
"Great picture!" or something like this. And why don’t we also add, say,
"Thank you!". Okay, so maybe the user’s posting “Thank you!” to something on the website.
So the user has made two posts, but this is an inactive user. Now, because both
.__len__() are defined,
.__bool__() takes precedence, and so if we use the
user in an
if statement, it’s going to be checking the
.__bool__() function, the
.__bool__() dunder method. And so in this case if we were to write something like
if user:, meaning that “If the
user is active,” then why don’t we go ahead and print something like
"We have an active user!".
All right. So, since you have a user that’s not active, that is what the
.__bool__() method is going to look at when you use it in any type of Boolean expression. And so in this case, we’re going to get the message that
We have an inactive user! Now, that still is the case, even though that this
user has a length of
Now, however, if you change the
.active property to
True so now we do have an active user, then if we run that same code that checks whether the
user is active or not, then in this case, we’re going to get that print statement that the user is an active user.
All right! So that’s just a quick example of how if you have both the
.__len__() and the
.__bool__() dunder methods defined on the class, the
.__bool__() method takes precedence and that’s what’s going to be checked in any type of Boolean expression involving an instance of the class. All right!
Become a Member to join the conversation.