You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
776 B
37 lines
776 B
2 years ago
|
class IterableStack:
|
||
|
def __init__(self):
|
||
|
self._lst = [1, 2, 3, 4, 5]
|
||
|
|
||
|
def __iter__(self):
|
||
|
return ReverseIterator(self._lst)
|
||
|
|
||
|
|
||
|
class ReverseIterator:
|
||
|
def __init__(self, _lst):
|
||
|
self._lst = _lst
|
||
|
self.position = len(_lst)
|
||
|
|
||
|
def __next__(self):
|
||
|
self.position -= 1
|
||
|
if self.position < 0:
|
||
|
raise StopIteration()
|
||
|
|
||
|
return self._lst[self.position]
|
||
|
|
||
|
|
||
|
# container = IterableStack()
|
||
|
# for v in container:
|
||
|
# print(v)
|
||
|
|
||
|
container = IterableStack()
|
||
|
it_container = iter(container) # container.__iter__()
|
||
|
while True:
|
||
|
try:
|
||
|
v = next(it_container) # it_container.__next__()
|
||
|
|
||
|
# тело фора
|
||
|
print(v)
|
||
|
# конец тела фора
|
||
|
except StopIteration:
|
||
|
break
|