Working With my_enum()
00:07 So just a few words about the setup right here. I’m still on VS Code, but in this lesson, you will work in the terminal. You can use any terminal you want. I’m using VS Code, and this has the advantage I can show you the file you just created in the last lesson above for reference. So one thing you have to make sure before you start, and that’s that you’re in the same folder that you created the files from the lessons before.
So to quickly show you what I mean by this, you have to make sure that the
my_enum.py file is in the same folder as you are working right now, because you want to import the
my_enum function from the
my_enum.py file in the Python interpreter. And that’s what we’re doing now.
python3 -m bpython, that’s the Python interpreter I’m using to test my syntax highlighting, but you can use the usual Python interpreter depending on your taste. So, as I just said, from
my_enum—that’s the file—import
So we run it. You see it prints the tuple, and the first part is the count, and the second part is the value of
seasons that is returned by
my_enum() at this point. Well, actually I should be a little bit more specific here.
I said it returns this item. Well, actually it yields this item. So what does this yielding mean? You can think about yielding as handing something out. So to show you what I mean by this, let’s repeat the
for loop again:
for val in my_enum(seasons, start=1):
This works because you’re creating a generator object every time you run the loop. However, when you assign
my_enum to a variable, the behavior is a bit different because the
yield statement will literally hand out the tuples of your generator object until it’s empty, and there is nothing to hand out anymore.
my_enum(), you pass in
seasons, let’s say the
start value is
1, you can, instead of accessing the
my_enum() function here, you can access the variable, and there it might get even more clear what this yielding/handout means.
default value, we’ll come to it in a second. But this iterator will be the
counted_seasons variable. However, currently our
counted_season’s variable is empty because all the tuples that were in there were yielded out, so you have to fill it up again.
counted_seasons, you always just get the next item back. That’s what your
my_enum() function yields at this point. So once this tuple is yielded, this
my_enum() generator object basically saves the state where it’s in right now and stops, until you touch it again.
next() function gets the next tuple yielded back—
2 Summer—and you can go through it until the end of the list. However, once there is nothing to yield back anymore, you will encounter
StopIteration error. That’s expected, and that’s why this
next() function contains this optional
default value. So if you don’t want to get back an error, you can say, well, if there is nothing in it anymore, then just return
So now it returns
None. I have no idea why there was this little visual glitch here, but here you see there is no error anymore. So to recap, the
my_enum() function that you created and just imported here behaves exactly the same like the
enumerate() function that is built in in Python.
Become a Member to join the conversation.