Hacker News new | ask | show | jobs
by rahimnathwani 2825 days ago
"it is an iterator (like a range or list also are)"

No, range is not an iterator. Try this in either python 2 or 3:

  a = range(10)
  a.next()
It will fail. Now try this:

  a = range(10)
  my_iterator = a.__iter__()
  my_iterator.next()
"Iterators are anything that has a __next__() method"

Right, and range(10) does not have a __next__() method, so it's not an iterator. It's an iterable, which is anything which has a __iter__() method that returns an iterable.

In python 3, range(10) returns a range object (not a list). Because it's an iterable and not an iterator, it doesn't get 'used up'. For example, try this (in Python 3 only):

  a = range(1000000000) # returns instantly, as it's not building a list
  a[3] # returns 3
  a[3] # returns 3 again