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