1

Why does an iterator in python 3 support __next__ and not next? Is it not supposed to be called directly but only while doing :

 for i in iterator:
      dosomething(i)

I have a use case where I would like to call next. For example using itertools.count for a stream of increasing integers. I would like to do :

from itertools import count
    cnt = count(0)
    one(cnt.next())
    two(cnt.next()) 

Right now I can do the above by using __next__ which suggests to me that it is supposed to not be called externally?

Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24
Captain Jack sparrow
  • 959
  • 1
  • 13
  • 28

1 Answers1

3

In Python 3, use the global function next():

one(next(cnt))
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • @khelwood Well, 2.6+, but those who don't even have that face bigger problems than their iterator fu not being forward compatible. –  Oct 16 '14 at 20:26