Image we have this custom iterator class:
class Counter:
def __init__(self, low,high):
self.current=low-1
self.high=high
def __iter__(self):
return self
def __next__(self):
self.current+=1
if self.current < self.high:
return self.current
raise StopIterationError
Is it necessary to call iter()
on the object and then call next()
on it? (I don't want to use a for loop)
Obj=Counter(1,10)
i=iter(Obj)
print(next(i))
Or:
Obj=Counter(1,10)
print(next(Obj))
Both seem to work.