0

I am trying to get familiar with python generators. I was playing with the range() function, which seems to return a range object, which my understanding is a generator. since it's a generator, then I should be able to do

a = range(10)
print(next(a))

but I get:

TypeError: 'range' object is not an iterator

why does it says it's not an iterator, since I can clearly do for i in range(10) ?

  • Ranges are not generators. People keep saying they are, but as this test demonstrates, those people are wrong. Ranges are sequences. – user2357112 Oct 18 '22 at 21:04

1 Answers1

-2

Iterators and generators are different types of things (objects). There's a pretty good explanation here

CSEngiNerd
  • 199
  • 6
  • While iterators and generators are different concepts, that fact doesn't answer the question, and stating it as an answer is misleading. Ranges are neither iterators nor generators. – user2357112 Oct 18 '22 at 21:09