22 lines
541 B
Python
22 lines
541 B
Python
class Squares:
|
|
def __init__(self, n):
|
|
self.n = n
|
|
self.current = 1
|
|
|
|
def __iter__(self):
|
|
return self
|
|
|
|
def __next__(self):
|
|
if self.current > self.n:
|
|
raise StopIteration
|
|
result = self.current ** 2
|
|
self.current += 1
|
|
return result
|
|
|
|
|
|
# 1. Prüfung mit list(Squares(4))
|
|
it = Squares(4)
|
|
print(list(it)) # 1. Durchlauf -> Ausgabe: [1, 4, 9, 16]
|
|
|
|
# 2. Was passiert beim zweiten Durchlauf desselben Iterators?
|
|
print(list(it)) # 2. Durchlauf -> Ausgabe: [] |