Python on the Train, Part 2

Python on the Train, Part 2

[I wrote this yesterday morning]

Me: there will be no more Python on train, and especially not at 6 am, the crack of dawn when I am basically still asleep anyway (yes, even me, who is usually bouncing around brightly, full of energy).

Also me:
  • I love coding
  • I can't sit still on the train
  • My mind is too full of everything to read any books at the moment
  • It makes the train journey go by sooooo fast but in a good way
  • I need to finish this course.
A pink bright desert image with bright colourful text summarising the contents of the blog post


Iterators and Generators

Finally, I am on to these.

I always loved iterators in JavaScript sooo much. Safe to say they were like my favourite thing.

Introduction to Iterables

Tbf I know what these do but always room to learn some more...

"In Python, an iterable is an object that's capable of being looped through one element at a time." 10/10 definition.

Has really helped me to grasp it some more and to understand it some more at a subconscious level. 

"We commonly use iterables to perform the process of iteration and it is the backbone for how we perform consistent operations on sets of data" - WOW. Wow, wow, wow, wow, wow. Where have these definitions been all by life? It's so beautiful. They're so beautiful and it's so quotable.

I like this because it highlights why loops are useful - apart from being less work - they mean that every single element on the iterable goes through the EXACT some process and gets the exact same code and logic applied to it. WOW. Thank you, wow.

As this course highlights, iterables are nothing new to me. And indeed, I have been working with them for years - wow.

Iterator Objects: __iter__() and iter()

Omg no this is way too much detail I'm sorry.

It's good to learn - like it's really good to know what's going on under the hood - like really really good - but the reason I write things out is to consolidate them and learn them forever. I'm learning from the best and they're telling me - don't worry too much about the stuff you don't need to know about that's going on under the hood. Just do what you need to get done.

Focus on what you need to do to do what you need to get done.

OKAY FINE

Having read through the lesson a bit more, I think we are gonna be using what they are teach a lot. And I do get it and it's not so bad so there we go.

There are two things we care about here:
  • the iter() function
  • And the built-in __iter__() method that comes with the new iterable object that we have created by calling iter() 
iter() basically converts the iterable item that we have passed in to an actual iterator object. But actually there is something more (and this gets sooooo complicated omg). iter() is calling a secret method inside of the iterable called __iter__() and therefore you could syntactically do the same thing twice, actually: 

Imagine a dictionary, lana_songs_and_albums:

lana_songs_and_albums = {"Yosemite": "Chemtrails Over The Country Club", "Bartender": "NFR", "Ultraviolence": "Ultraviolence"}

Now, supposing you want to use a for loop to print the song and album names - something like:

for song in lana_songs_and_albums:
    print(song + " is on the album " + lana_songs_and_albums[song])

But what actually happens in the for loop and what is it that can be done and what am I actually trying to illustrate here? Thanks.

Well let's say we want to turn the iterable, my dictionary lana_song_albums, into an actual iterator object. When the for loop gets started what's actually done here is:
  • iter(lana_songs_and_albums) get's called. But this actually calls the built-in __iter__() method inside of the dictionary.
  • Therefore, it must be the same thing as doing this - I think: lana_songs_and_albums.__iter__()
Okay so this is nice and this is cool but why bother teaching me this Codecademy please? Thanks.

So they can be used interchangeably! Okay, interesting, that's good to know...

From the course: "observe that both methods will be able to retrieve an iterator object, but it's always helpful to know that iter() uses __iter__() under the hood"! WOW! Wow.

Next up will be next() and __next__() - somehow, I am beginning to feel that I am spotting a pattern. Thanks...




Comments

Popular posts from this blog

Hello World

Yosemite

Two Lines of Thought, Makes Me Wonder What I'm Missing