Python3 Intermediate Tutorial 5 - Iterators & Generators

preview_player
Показать описание


If you like what you see be sure to subscribe and thumbs up!
Рекомендации по теме
Комментарии
Автор

It's worth noting that everytime you call a for loop - it goes to the __iter__ and __next__ - and that here we are basically creating a new for loop that will be in reverse. The len(data) will be the actual number of characters, where since the index starts at 0, the last index will be len(data)-1, this is why in the __next__ method/function you check if the index is equal to 0, but return the index -1. Meaning - once you reach the length of 1 - you will return the final char, which will be data[0].

Also the generator is the code: for index in range(len(data)-1, -1, -1) - so the first parameter is the last index, 2nd is when to stop, and the 3rd is how much to add/decrease. In your case you start on the index 8 (since the string length is 9), stop when reaching -1, and go down 1 every time. The yield "pauses" or "saves" this loop, and returns the value, and it continues everytime the "outside" loop (for char in rev) calls it again.

RealMcDudu
Автор

WOW! A python video with a narrator who doesn't have an Indian accent!

davidk
Автор

Great video. I would say may be a touch better if you also did from index 0 to the end of the object being iterated just for thoroughness. Still well done and I appreciate the content, thank you very much for sharing.

brainkill
Автор

Hello - I was thinking about this iterator example.  I wondered why the ___iter___() method of the class just returns the class object.  Then I realized that this iterator will only work once ! The self.index will remain zero for the second call, and the iterator will return immediately without iterating anything.  Testing bore this out. 

The purpose of the ___iter___() method must be to initialize the iterator *each time*, and it must be automatically called upon each 'for' (or other such) statement.  To fix it, one can just add a line like 'self.index = len(self.data)' to the ___iter___() method, before it returns.  Tests fine.

agnichatian
Автор

Can you explain about why did you use "-1" i mean what does "-1" means?

utkuhangol
Автор

So in the example where Reverse is a function (not a class), what is returned in: rev = Reverse ('Drapsicle') ? It's not an object with a length, so how does the subsequent for loop (for char in rev) know when to stop?

bennguyen
Автор

HI, I am new to python but I like to have hacking tutorials with python 2 please. U are really good in teaching and I can't find anyone else. Thank You!

mennekep
Автор

I use Eclipse and PyDev . When defining the Main function it asks me to enter "self" as a parameter .Also when running the " Main()" it gives me another error with :
Main()
TypeError: Main() takes exactly 1 argument (0 given)

TexaSol
Автор

I got the error of:

"TypeError: instance has no next() method"

And it specifically points out line 22 and line 18. Any idea what to do different? Or this this because I'm using Python 2.7 and you're using Python 3.x?

rhettmelton
Автор

But actually not all iterators we can call len() upon, so the examples are good for simplicity but not in terms of practicality.

yukewang
Автор

err...you might want to teach people how to do it the pythonic way instead of the C way; range(len(foo)) in python should be expressed as i, value in enumerate(foo)

MaddinW
Автор

Hi I want to learn data structure and algorithms please help me if anything good tutorial

loveofjava
Автор

you've explained nothing new, all of this is written in the official python documentation

dimashur