forked from twtrubiks/python-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__iter__tutorial.py
More file actions
35 lines (32 loc) · 768 Bytes
/
__iter__tutorial.py
File metadata and controls
35 lines (32 loc) · 768 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
class Product(object):
def __init__(self):
self.products = [
{
'id': 1,
'count': 1,
'price': 10
},
{
'id': 2,
'count': 2,
'price': 20
},
{
'id': 3,
'count': 3,
'price': 30
},
{
'id': 4,
'count': 4,
'price': 40
}
]
def __iter__(self):
for product in self.products:
product['total'] = product['count'] * product['price']
yield product
if __name__ == "__main__":
p = Product()
for result in p:
print(result)