In the last few lessons, we’ve taken a look at how
or works with Boolean expressions, common objects, and a mix—one Boolean, one object. The behavior is basically the same for each case.
We evaluate the first expression first, and if that’s
True, we know what the result of the entire expression is going to be, and if that’s
False, we evaluate the second expression.
Python actually follows that quite literally with what’s called short-circuit evaluation, also sometimes referred to as lazy evaluation. If the first expression is
True, Python will not even evaluate the second expression.
There’s no need! The result of an
or operation is completely determined if the first operand is
00:52 and the article on Real Python has a great example to show this, and I’m going to go through that with you.
So, we define two functions that print that they are running and return a Boolean value. This first one is the
true_func() that’s going to say that we’re running it, and then it returns
True. The second one is the
false_func(), which is going to print that it’s running, and then it returns
False, but they both print out a statement to indicate that they are running, and that’s what we’re going to observe when we do this.
from, very creative name,
python_or_short, for short-circuit evaluation, let’s import both of those functions. I’m going to say
true_func() or true_func(). Now, to determine whether this first operand is
False, it’s actually going to have to run that function. And when it does that, we’re going to see that that function is running and we know that it returns
We’re not going to see the second function operating at all. We will know by the absence of a second statement that the
true_func() is running, that it didn’t run.
So, we’re running the
true_func() just once—that’s this first one—and once Python determined that the whole thing was
True, it didn’t bother with the second function.
If I do
true_func() or false_func(), again we will see that the
false_func() won’t run because Python will have completely determined the value of this
or operation, having finished running the
true_func() and figuring out that it was
Now if I switch those, if I call the
false_func() first and then the
false_func() has to run first because Python wants to determine whether that operand is
False. When it determines that it’s
False, it’s going to have to give back the value of the second operand. Well, it’s going to have to run that function to see what its value is, so we’re going to see
false_func() running and then we’re going to see
true_func() running, and then because
True, that’s what Python is going to return as the result of that operation.
So we see
false_func() ran, it returned
False, so Python had to evaluate the right operand to figure out what to return because we know we’re returning it. So, it evaluates it and it returns
And if I call
false_func() or false_func(),
we’re going to see both of them run. We have to run the first one to see if it’s
False. Since it returns
False, we return the value of the second one, but we have to run that to see what its value is.
So if we do this, we should see both
false_func() calls run. There’s the first time it ran, returned
False, runs again for the second operand, it returned
False, and it’s returning the
False that it got from the second function call.
Now, as a programmer, you can take advantage of that behavior when you write your
or statements. One of the things that you can do is if you have among your two conditions one of them that you know is going to be
True most of the time, put it first!
That way, the
or operation is completed quickly most of the time, and it’s only those rare times where it turns out to be
False that you even have to evaluate the second operand.
This will improve your program’s performance because it’s avoiding some computations that it doesn’t have to do. On the other hand, if one of your operands involves a function that’s somewhat complex and that’s going to take a long time to run, maybe put that as the second operand and then hope that the first operand is easier and it evaluates to
True enough times that you don’t have to even do the more complicated process to determine the value of the
And then it’s only when the first operand is
False that you would call the second operand, cutting down how many times you would have to use it. So there’s a couple of ways that you, as a programmer, can order the two things in your
or statement to help make your program more efficient.
Let’s look at one more thing as it applies to short-circuit evaluation, and that’s if you use
or to connect multiple expressions. Well, in terms of Boolean logic, this expression is
True if at least one of the four expressions is
But remember, in Python, the evaluation is going to be done from left to right. So if
True, evaluation stops and
a is returned.
If it’s a Boolean expression, we return
True. If it’s an object, we return that object. Then technically, if it’s
False, then the result is the other side of the
or, but that’s
b or c or d, which then needs to get evaluated.
And again, what happens? We check to see if
True, the evaluation stops and
b is returned. If not, we go on to
c or d.
So, the net effect of this is the first
True expression is returned when we look at it from left to right until and unless we get to the very last one, which is returned no matter what if all of the ones to the left of it were
In the next lesson, we will summarize all of the syntactical use of the Python
Become a Member to join the conversation.