-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator_iterator.py
60 lines (42 loc) · 1.07 KB
/
generator_iterator.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
"""
Simple study related to iterators and generators
"""
import random
# Example 1
def throw_dices(n):
dices = []
for _ in range(0, n):
dices.append(random.randint(1, 6))
return dices
# Example 2
class Trhow_Dices(object):
def __init__(self, n):
self.n = n
self.counter = 0
def __iter__(self):
return self
def __next__(self):
if self.counter < self.n:
self.counter += 1
return random.randint(1, 6)
else:
raise StopIteration
def throw_dices_yield(n):
# yield blocks function execution until next method is called
print('starting function')
for _ in range(0, n):
print('starting for loop')
yield random.randint(1, 6)
print('exit for loop')
print('end of function')
if __name__ == '__main__':
# Example 1
for dices in throw_dices(1):
print(dices)
# Example 2
for dices in Trhow_Dices(1):
print(dices)
#Example 3
for dices in throw_dices_yield(10):
print(dices)