Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Range:
- print("Class creation")
- def __init__(self, start_stop, stop=None, step=None):
- print("__init__")
- if stop is None:
- self.start = 0
- self.stop = start_stop
- else:
- self.start = start_stop
- self.stop = stop
- self.step = step or 1
- def __iter__(self):
- print("__iter__")
- current = self.start
- while self._in_range(current):
- yield current
- current += self.step
- def __contains__(self, value):
- print("__contains__")
- in_sequence = (value - self.start) % self.step == 0
- return self._in_range(value) and in_sequence
- def _in_range(self, value):
- return value < self.stop if self.step > 0 else value > self.stop
- def __getitem__(self, index):
- print("__getitem__")
- value = self.start + self.step * index
- if not self._in_range(value):
- raise IndexError("Index is out of range")
- return value
- __call__ = __getitem__
- def index(self, index):
- print("__index__")
- return self[index]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement