-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassfib.py
More file actions
44 lines (33 loc) · 902 Bytes
/
classfib.py
File metadata and controls
44 lines (33 loc) · 902 Bytes
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
43
class cfib:
def __init__(self,maxnum):
self.maxnum = maxnum
self.a = 0
self.b = 1
def __iter__(self):
return self
def __next__(self):
self.a,self.b = self.b,self.a + self.b
if self.a > self.maxnum:
raise StopIteration()
return self.a
def __getitem__(self, item):
if isinstance(item,int):
for _ in range(item):
self.__next__()
return self.__next__()
elif isinstance(item,slice):
L = []
start = item.start
stop = item.stop
if not start:
start = 0
for x in range(stop):
if x >= start:
L.append(self.__next__())
self.__next__()
return L
fib = cfib(1000000)
print(fib[6])
print(fib[8:10])
for i in fib:
print(i)