Advertisement
DeaD_EyE

Range class, not complete, not tested enough

Nov 19th, 2024 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. class Range:
  2.     print("Class creation")
  3.  
  4.     def __init__(self, start_stop, stop=None, step=None):
  5.         print("__init__")
  6.         if stop is None:
  7.             self.start = 0
  8.             self.stop = start_stop
  9.         else:
  10.             self.start = start_stop
  11.             self.stop = stop
  12.         self.step = step or 1
  13.  
  14.     def __iter__(self):
  15.         print("__iter__")
  16.         current = self.start
  17.         while self._in_range(current):
  18.             yield current
  19.             current += self.step
  20.  
  21.     def __contains__(self, value):
  22.         print("__contains__")
  23.         in_sequence = (value - self.start) % self.step == 0
  24.         return self._in_range(value) and in_sequence
  25.  
  26.     def _in_range(self, value):
  27.         return value < self.stop if self.step > 0 else value > self.stop
  28.  
  29.     def __getitem__(self, index):
  30.         print("__getitem__")
  31.         value = self.start + self.step * index
  32.         if not self._in_range(value):
  33.             raise IndexError("Index is out of range")
  34.         return value
  35.  
  36.     __call__ = __getitem__
  37.  
  38.     def index(self, index):
  39.         print("__index__")
  40.         return self[index]
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement