My Simple Range example 3
This commit is contained in:
parent
ea6431687e
commit
c7da42383d
28
python-temel/MySimpleRange.3.py
Normal file
28
python-temel/MySimpleRange.3.py
Normal 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)
|
||||
|
||||
for i in r:
|
||||
print(i, end=' ')
|
||||
print()
|
||||
for i in r:
|
||||
print(i, end=' ')
|
7
python-temel/MySimpleRange.3.py~
Normal file
7
python-temel/MySimpleRange.3.py~
Normal file
|
@ -0,0 +1,7 @@
|
|||
r = MySimpleRange(10)
|
||||
|
||||
for i in r:
|
||||
print(i, end=' ')
|
||||
print()
|
||||
for i in r:
|
||||
print(i, end=' ')
|
Loading…
Reference in New Issue
Block a user