-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyRange.py
More file actions
31 lines (24 loc) · 1.03 KB
/
MyRange.py
File metadata and controls
31 lines (24 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# we are going to create our own range() method
# range() method is an iterator
# iterable are not iterator BUT iterator are iterable
class MyRange:
def __init__(self, start, end) -> int:
self.value = start
self.end = end
def __iter__(self):
"""
Creating __iter__() so our MyRange is iterable
for our MyRange to be iterator, __iter__() has to return iterator
i.e: it has to return an object that has __next__()
since we will create __next__() in this class we will return the same object(i.e self)
"""
return self
def __next__(self):
if self.value > self.end: # since range() exclude end value so our custom method will also exclude end value
raise StopIteration # it value > end than we will raige StopIteration Error
current = self.value # storing current self.value
self.value += 1 # increment the value
return current
# as we can see below our own range() is been created
for i in MyRange(1, 10):
print(i)