Exercise 8.1 - Solution
(b) Supporting Iteration
# portfolio.py
import stock
class Portfolio(object):
def __init__(self,filename=None):
self.holdings = stock.read_portfolio(filename)
# ---- ADDED
def __iter__(self):
return self.holdings.__iter__()
# ---- END ADDED
def cost(self):
return sum([s.shares*s.price for s in self.holdings])
if __name__ == '__main__':
port = Portfolio('Data/portfolio.csv')
for s in port:
print s
[ Back ]