An instance of a stock holding consisting of name, shares, and price.
| 1 | # stock.py |
| 2 | |
| 3 | class Stock: |
| 4 | ''' |
| 5 | An instance of a stock holding consisting of name, shares, and price. |
| 6 | ''' |
| 7 | def __init__(self, name, shares, price): |
| 8 | self.name = name |
| 9 | self.shares = shares |
| 10 | self.price = price |
| 11 | |
| 12 | def __repr__(self): |
| 13 | return f'Stock({self.name!r}, {self.shares!r}, {self.price!r})' |
| 14 | |
| 15 | def cost(self): |
| 16 | ''' |
| 17 | Return the cost as shares*price |
| 18 | ''' |
| 19 | return self.shares * self.price |
| 20 | |
| 21 | def sell(self, nshares): |
| 22 | ''' |
| 23 | Sell a number of shares |
| 24 | ''' |
| 25 | self.shares -= nshares |
| 26 |