Skip to content

Instantly share code, notes, and snippets.

@izxxr
Last active December 6, 2021 15:35
Show Gist options
  • Save izxxr/5c93c4b4e6a4970ef453f453adb53b31 to your computer and use it in GitHub Desktop.
Save izxxr/5c93c4b4e6a4970ef453f453adb53b31 to your computer and use it in GitHub Desktop.
from typing import Optional
class ShittyRange:
"""A shitty implementation of range()."""
__slots__ = ('start', 'end', 'step', '_current')
start: int
end: int
step: int
_current: int
def __init__(self, start: int, end: Optional[int] = None, step: int = 1) -> None:
if end is None:
# ShittyRange(x) -> x is ending point, We take "0" as
# starting point.
self.start = 0
self.end = start
else:
# ShittyRange(x, y) -> x and y are starting and ending points
# respectively.
self.start = start
self.end = end
self.step = step
def __iter__(self):
self._current = self.start
while self._current != self.end:
yield self._current
self._current += self.step
def __repr__(self) -> str:
if self.step != 1:
return f'ShittyRange({self.start}, {self.end}, {self.step})'
return f'ShittyRange({self.start}, {self.end})'
# Example:
#
# for i in ShittyRange(100):
# print(i)
#
# is equivalent to:
#
# for i in range(100):
# print(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment