0

I understand the concept of the Iterator and Generator in Python. But I have below queries:

When I use a generator I get a value one at a time, however according to the iterator concept it gives the output as a whole. So why does the iter() function has next() sub function? for example

rt = "default"
>>> type(iter(rt))
<type 'iterator'>
>>> iter(rt).next()
'd'

So, when we do a iter().next() we are getting single output at a time, so how it is different from generator?

Thank you,

user3521180
  • 1,044
  • 2
  • 20
  • 45
  • Are you asking what is difference between a generator and an iterator? – DeepSpace Sep 16 '19 at 09:52
  • An iterator is an object that yields items one at a time. A generator *returns* an object that acts as an iterator. Does that clear things up? – Tom Zych Sep 16 '19 at 09:54
  • @TomZych, I guess you are saying the other way around. The generator yields an object one at a time not the iterator. Anyway, what I meant was from my example I have one object at a time through iterator, and the same feature is present in generator as well. So apart from memory advantage I do not see a reason why should I use Generator over iterator. I hope my question is clear now. – user3521180 Sep 16 '19 at 10:21
  • Possible duplicate of [Difference between Python's Generators and Iterators](https://stackoverflow.com/questions/2776829/difference-between-pythons-generators-and-iterators) – Diptangsu Goswami Sep 16 '19 at 10:34
  • @DiptangsuGoswami I am not asking difference between Generator and Iterator. – user3521180 Sep 16 '19 at 10:35
  • I’m not really sure what you’re asking, but you don’t seem to be very clear on the relationship between iterators and generators. You may find these docs useful; see the sections “Iterators” and “Generators”. https://docs.python.org/3/howto/functional.html – Tom Zych Sep 16 '19 at 22:44
  • Then what do you mean bt "it" in "how it is different from generator?" – Stop harming Monica Sep 17 '19 at 09:34

0 Answers0