-
Notifications
You must be signed in to change notification settings - Fork 0
/
generators.py
42 lines (33 loc) · 838 Bytes
/
generators.py
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
31
32
33
34
35
36
37
38
39
40
41
42
def first(n):
num = 0
while num < n:
yield num
num += 1
generator = first(10000)
print(next(generator))
print(next(generator))
print(next(generator))
print(next(generator))
class FibonacciGenerator:
def __init__(self, limit):
self.first = 1
self.second = 2
self.counter = 1
self.limit = limit
def __iter__(self):
return self.counter
def __next__(self):
if self.counter >= self.limit:
raise StopIteration
result = self.first
self.first, self.second = self.second, self.first + self.second
self.counter += 1
return result
print()
generator = FibonacciGenerator(10)
print(next(generator))
print(next(generator))
print(next(generator))
print(next(generator))
print(next(generator))
print(next(generator))