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.
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
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
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
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
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!
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
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.
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
Become a Member to join the conversation.