Thursday Mornings With Python, Part 5
Thursday Mornings With Python, Part 5
Generators
This week is a bit different
- I have my meeting where I talk about this earlier than usual so I won't get as much done before
- I have spent most of the morning struggling through with a project whose instructions make no sense (which I will go through in my review meeting later)
- I have tried it THREE TIMES by myself
- I went through the instructions with an Engineer II (I am an Engineer I) and even then it didn't make sense so I am gonna try with my Senior...
- I had a stand-up that overran this morning
So for some of this morning I am going to be finishing up this project on iterators.
But I can get started on my lesson on generators a bit more at least and maybe do a bit more after my meeting... so let's go...
Introduction to Generators
It says here that:
- Generators allow for the creation of iterators without having to implement __iter__() and __next__()
- Wait really what? I hadn't been expecting this.
- I have just taught myself generators from CodeWars problems so haven't really seen this context before yet I am so sorry
Generators
- Improve code readability
- Save memory by allowing for iterative access of elements
- Allowing for the traversal of infinite streams of data
There are two types of generators in Python:
- Generator functions
- Generator Expressions
Okay what wow I didn't know this.
I had only worked with Generator Expressions before so I didn't know that generator functions were a thing okay yes wow.
"Both of them return a generator object that can be looped over similar to a list, but unlike a list, the contents of the generator object are not stored in memory, allowing for complex and even infinite iteration of data." Oh WOW.
I knew about the generator object because I have used those before but again it is a bit mystical and a bit complex to me all in all and in the end. There is a really cool image for what we will cover in the course so let me put it here - I hope that this is okay - thanks.
yield vs return
"Generator functions are similar to regular functions except that they must return an iterator. But instead of using a return statement, generator functions use an expression called yield."
"But instead of using a return statement, generator functions use an expression called yield."
But instead of using a return statement, generator functions use an expression called yield. But instead of using a return statement, generator functions use an expression called yield. Ah yes.
Been there.
"So how does yield differ from a return statement? Well, any code that is written after a yield expression will execute on the next iteration of the iterator. Code written after a return statement will not execute." I got these.
I always got them.
Why is this lesson just confusing me? Oh I am so lost omg. I thought I get these but I just do not get them omg.
I have been told to play around with code
When I get things. So let me please have a go.
Here is the code that I wrote:
def class_standing_generator():
yield 'Freshman'
yield 'Sophomore'
yield 'Junior'
yield 'Senior'
class_standings = class_standing_generator()
for class_standing in class_standings:
print(class_standing))
I GET NOTHING.
Ahem
Okay so this line
class_standings = class_standing_generator()
creates a generator object. Chat GPT is being helpful, here's what it says:
"This creates a generator object. The function class_standing_generator
doesn't run immediately when you define it; instead, it creates the generator object, which you can then iterate over or call with next()
.
The generator object (class_standings
) behaves like an iterator, meaning you can either:
- Use
next(class_standings)
to get one value at a time. - Use a loop (e.g.,
for
loop) to iterate over all the yielded values.
Breakdown:
- Generator function:
class_standing_generator()
defines the behavior of a generator usingyield
. - Generator object: When you call the function like this:
class_standings = class_standing_generator()
, it returns a generator object. This object can be iterated or called usingnext()
to retrieve values one by one."
And again:
"
yield
:- Purpose: It is used to pause the function and send a value back, but the function can continue from where it left off the next time it's called.
- Effect: It turns the function into a generator (a special kind of iterator).
- Used in: Generator functions, where you need to produce a series of values over time."
I think I am going to have to revisit this with someone else. It will be more helpful. Thanks.
Comments
Post a Comment