-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclasses_and_iter.py
More file actions
43 lines (30 loc) · 821 Bytes
/
classes_and_iter.py
File metadata and controls
43 lines (30 loc) · 821 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
class MontyHallProblem(object):
def __init__(self):
self.words = ['Monty', 'Hall', 'Problem]
self.idx = 0
def __iter__(self):
self.idx = 0
return self
# return MontyHallProblem()
def __next__(self):
if self.idx == len(self.words):
raise StopIteration()
word = self.words[self.idx]
self.idx += 1
return word
""" By altering __iter__ method,
can change behavior of the class when more than one instance is called
self.idx = 0
return self
OR
return self
OR
return MontyHallProblem()
"""
m = MontyHallProblem()
it1 = iter(m)
print(next(it1))
print(next(it1))
print('++++++++++++')
it2 = iter(m)
print(next(it2))