Jul-06-2017, 08:27 PM
It's just like range(), except it only generates numbers as you ask for them (instead of all at once). If you wanted to see what it looks like in practice, here's a bad version I just made up:
>>> class xrange: ... def __init__(self, min=0, max=None, step=1): ... if max is None: ... max = min ... min = 0 ... self.max = max ... self.step = step ... self.current = min ... def __iter__(self): ... return self ... def __next__(self): ... value = self.current ... self.current += self.step ... if self.step > 0: ... if value >= self.max: ... raise StopIteration() ... else: ... if value <= self.max: ... raise StopIteration() ... return value ... >>> for i in xrange(5): ... print(i) ... 0 1 2 3 4 >>> for i in xrange(2, 12, 3): ... print(i) ... 2 5 8 11