My Simple Range example 4

This commit is contained in:
Mert Gör ☭ 2023-09-02 13:07:00 +03:00
parent c7da42383d
commit 3ae332c74b
No known key found for this signature in database
GPG Key ID: 2100A876D55B39B9
1 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,28 @@
class MySimpleRange:
def __init__(self, stop):
self.stop = stop
def __iter__(self):
return MySimpleRangeIterator(self.stop)
class MySimpleRangeIterator:
def __init__(self, stop):
self.stop = stop
self.count = 0
def __iter__(self):
return self
def __next__(self):
self.count += 1
if self.count > self.stop:
raise StopIteration
return self.count - 1
r = MySimpleRange(10)
iterator = r.__iter__()
for i in iterator:
print(i, end=' ')
print()
for i in iterator:
print(i, end=' ')