Links: PROGRAMMING - PYTHON
Rel: python standard library builtins
Ref:
Tags: #public
moves __next__ manually if __iter__ is defined;
is effectively a step in a for loop
- generator expression: (i for i in range(100))
range as a class:
class RangeExamp:
def __init__(self, end, step=1):
self.currt = 0
self.end = end
self.step = step
def __iter__(self):
return self
def __next__(self):
if self.curr >= self.end:
raise StopIteration()
else:
return_val = self.current
self.curr += self.step
return return_val
range as a function:
def range_func(end):
curr = 0
while curr < end:
yield curr
curr += 1