For more information on concepts covered in this lesson, you can check out How to Use Generators and yield in Python
Short Circuiting With any() and or
In this lesson, you’ll be looking at short circuiting. What is short circuiting? Well, first off, both
any() short circuit, so this behavior is the same in both of them.
And what it means is that if there are lots of values, they will both return on the first truthy value. So say you were using
any(), and you had a list of ten values, and the second value was
All the rest are
any() will look at the first value, see it as
False, go to the next value, see that it’s
True, and then it will return
True, and it won’t check the rest of the values.
00:33 This is what short circuiting means. Now to demonstrate this behavior, you can do this with generators because generators you can customize to print something to the console every time a value is accessed or generated. And so in this way, you’ll be able to demonstrate short circuiting behavior and review generators.
Okay. So what are generator functions? Now, this lesson isn’t going to go into much detail about what generator functions are and how they work. It’s just going to go through a quick example now of a typical generator function, a simplified generator function, and it will show you how it works and then it will use these concepts to build another generator function that will illustrate the behavior of short circuiting in
any(). if you want more details on generator functions, check out the article linked below, called Introduction to Python Generators.
Here’s an example of a Python generator function. As you can see, it’s called
numbers_from_zero_to_nine(). What it does is it loops
for number in range(10), which means that
number will be
0 in the first iteration and
9 in the last iteration, going through each number in between, and here it yields a number.
yield keyword that replaces the
return keyword in normal functions, and it is what makes it a generator function. What this means is that when execution reaches this line, but it says
yield number, it will return that number, and it will pause itself until you ask it for another number, and then it will carry on execution, which means it will go back up to the
for loop, define
1, and then yield
1 and return it and stop until you request another number, on and on until
9. The way you use a generator function is that you call it, and when you call this function, it will return a generator iterator.
02:33 So let’s start using this.
Okay. So now, if we look at the generator, what’s in there? It says it’s a generator object. How do you use this generator object? Well, you call the in-built
next() function on the generator—
or better said, you call the
next() function, and you pass the generator as an argument to it. And what this will do is it will ask the generator for its first value—the first value that it yields, which is
And then if you call it again, it will return
1, and on and on and on, all the way until
And then it will say—it will raise an error once it reaches the end. Functions that take in iterators, like generators, will know how to deal with this error, and it won’t raise it, like
Okay, so now you’re going to build a generator function to illustrate the short circuiting behavior of
any(). Okay, so what is this generator doing?
03:45 It takes in an iterable as an argument, and it creates a generator with that. So with this, you’ll be able to pass in a list, and it will make a generator of that list.
So it will go through each item in the list, and it will print something to the output. It will say
Yielding the item that it is yielding, and then it will yield, and then it will stop until you call the next item.
So to make a generator from that, say you have this iterable of numbers, and then you call
generate_items with that iterable, and it’ll create the generator that you want. It makes it easy to create generators.
04:24 But the key aspect of this generator is that every time you call an item from this generator,
it will output a message saying that it is
In this way, you can pass it into a function like
any() that will go through each of these and yield each of these items, and you can see how far it gets into the list, and that’s where short circuiting comes in. Okay, so say you have this example here, one that you saw right at the start:
So it’s just a list of
False values. The third to last is
True. So you’ll notice that
any() calls all the items until
True, and then it doesn’t check the last two.
05:14 So first, create the generator with the function that you defined, and pass it in
05:24 the variable that contains the list.
So now you can see that the generator is created, and if you pass that generator into
any(), you’ll see that it returns
True at the end, which is what you expect because there is one
However, you see that it goes through each item and then once it gets the
True, it stops. And that’s short circuiting, basically saving a bit of energy and not having to check anything. So if the first item was
any() would only check the first item. This is the same with
or. If you have one condition
or another condition
or another condition, if the first item is
True, then that is the item that will be returned, and the rest of the items won’t even be checked.
And there you have it: short circuiting. In this lesson, you’ve looked at what short circuiting means, and you’ve also used generators to demonstrate how
any() short circuits.
Bear in mind that
or will also short circuit, except it won’t return a
False value at the end. It will return the first truthy value itself, so if you’re comparing numbers, it will return the actual number and not
06:40 Next up is a summary of this course. You’re at the end, so well done for getting this far.
Become a Member to join the conversation.