Short-Circuit Evaluation
00:00
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.
00:13
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.
00:25
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.
00:43
There’s no need! The result of an or
operation is completely determined if the first operand is True
,
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.
01:00
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
.
01:22
So the true_func()
returns True
, the false_func()
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.
01:34
So, 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 True
or 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 True
.
02:07
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.
02:20
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.
02:34
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 True
.
02:56
Now if I switch those, if I call the false_func()
first and then the true_func()
,
03:06
the false_func()
has to run first because Python wants to determine whether that operand is True
or 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_func()
returns True
, that’s what Python is going to return as the result of that operation.
03:34
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 True
.
03:46
And if I call false_func() or false_func()
,
03:53
we’re going to see both of them run. We have to run the first one to see if it’s True
or 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.
04:05
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.
04:23
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!
04:41
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.
04:51
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 or
operation.
05:21
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.
05:41
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 True
. It’s False
otherwise.
05:59
But remember, in Python, the evaluation is going to be done from left to right. So if a
is True
, evaluation stops and a
is returned.
06:07
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.
06:25
And again, what happens? We check to see if b
is True
. If b
is True
, the evaluation stops and b
is returned. If not, we go on to c or d
.
06:35
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 False
.
06:52
In the next lesson, we will summarize all of the syntactical use of the Python or
operator.
Become a Member to join the conversation.