Python on the Train, Part 3

Python on the Train, Part 3

next() and StopIteration

Oh wait! Oh hold on a minute.
I've seen these two before.

I've seen these two before, haven't I. They are part of the iterators lesson. And now they are teaching them to us again in the generators one. Clearly they are part of the same process. I kind of knew this anyway especially from my Senior's amazing explanation of yield vs return. But it's still very helpful to learn them anyway and to see them anyway again please - thanks.

"Generator functions return an iterator object that contains traversable values." Generator functions return an iterator object that contains traversable values.

Generator functions return an iterator object that contains traversable values.

A pink and purple image of a city and a bridge by night. Text reads: Python on the Train, Part 3

Retrieving the next value from a generator object

"To retrieve the next value from a generator object, we can use the Python built in function next() which will cause the generator function to resume its execution until the next yield expression is found.

OMG YES I GET IT. I GET IT OMG OMG OMG. OMG YES I GET IT. YES YES YES. 

I GET IT YES.

This is exactly what I went over with my Senior. I get it thank you so so much.

I get it. Thanks.

OMG YES.

OMG

"If no additional yield expressions are found in a generator function, that means the code has finished and a StopIteration is raised." OMG YES.

THIS EXACTLY WHAT MY SENIOR HAS SAID. 

YES. I GET IT. I GET IT. THANKS.

NO

Wait. Hold on.

No. Hold on a minute. Hang on a minute. Noooo. WHY DOES THIS HAVE TO BE SO HARD PLEASE? NO.

Hang on a minute. 

I am having a panic attack. I have just seen something that looks really really hard. 😂

Okay, let's just take it line by line, please.

Line by line, please.

Thank you. ARRGH.

GENERATORS IN LOOPS NOOOO WHY

"Generator functions are not just limited to single yield statements.

They can also include loops where the yield occurs."`

NOOOO THIS IS IT. I can't take it anymore.

Why is this so hard. 😂 This makes no sense. 

Okay maybe I am just a bit burnt out to be fair. I coded like a mad person yesterday. Only during working hours of course.

But still. I produced soooo much code. Omg.

I think that I am just a bit tired and exhausted. Nothing else is going in.

But let me try anyway.

"I SHALL NOT BE THERE. I SHALL RISE AND PASS."

Let me try out the code.

"To see this in action"

Eww gross yuck.

So just a reminder (not gross anymore and not yuck - I LOVE this). When we loop through a dictionary: the value that we loop through will be automatically by default the KEY of the property, of the KEY-VALUE PAIR. OMG. I get it now I get it now. Omg. It has taken me years to get it - because it is so hard. Even Objects in JavaScript which are similar to dictionaries in Python - that take me ages to get omg. And so this is all a part of this.

I CAN'T deal with this anymore I think I need to try and start this again please nooooo

Actually do you know what. I think I might have gotten it all. Let me type through the actual code and run it, and then see if I have to read it all through again. Noooooooooo.

The Universe is on my side! Just as I was about to attempt this code the train drove into a tunnel. Lol. Saved. Just kidding btw the code should be pretty easy.

Sometimes it is easier to code things out. Then to read about them.

Right? This is what my Senior has said to me as well. When I try to explain complex problems to him he tells me he just wants to see the code.

How exciting, right?

HERE IS MY CODE - I am beginning to get it - omg omg 

def student_standing_generator():
  student_standings = ['Freshman','Senior', 'Junior', 'Freshman']

  for student in student_standings:
    if student == 'Freshman':
      yield 500
  
standing_values = student_standing_generator()

print(next(standing_values))
print(next(standing_values))
So, SO FAR, if we break it down line by line what the code does is THIS:
  • It creates a function
  • Inside of that function are a list of levels of different students
  • We then have a for loop inside of it
  • AND CRUCIALLY THAT FUNCTION ONLY YIELDS WHEN WE HIT A 'FRESHMAN' VALUE
  • SO IT MAKES SENSE THAT THE OUTPUT OF RUNNING THIS IS:
500
500
  •  BECAUSE: WE ONLY YIELD WHEN WE REACH A 500
  • AND OTHERWISE THE FUNCTION JUST KEEPS RUNNING
  • Like when I run a debugger - LIKE WHEN I RUN THE PYCHARM DEBUGGER!!! 
    • IT IS JUST LIKE PRESSING CONTINUE ON A PYCHARM DEBUGGER
    • OMG OMG 
    • AND EACH YIELD IS LIKE A BREAKPOINT OMG OMG
    • AND WHEN WE REACH A YIELD STATEMENT IT IS LIKE HITTING A BREAKPOINT IN OUR DEBUGGER - OMG, OMG, OMG.
And then if I run print(next(standing_values)) one more time? A StopIteration error is raised. Of course.

Next Up

Next up is Generator Expressions... which I love btw. Thank you. I learned them loads while doing loads and loads of Codewars exercises. 

I am so excited I might even do them on the train home tonight but I want to write up my goals so who knows? Let's see.

Thank you

Comments

Popular posts from this blog

Hello World

Yosemite

Where To Hide A Star