forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstock.py
32 lines (26 loc) · 762 Bytes
/
stock.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
# stock.py
from typedproperty import String, Integer, Float
class Stock:
'''
An instance of a stock holding consisting of name, shares, and price.
'''
name = String('name')
shares = Integer('shares')
price = Float('price')
def __init__(self,name, shares, price):
self.name = name
self.shares = shares
self.price = price
def __repr__(self):
return f'Stock({self.name!r}, {self.shares!r}, {self.price!r})'
@property
def cost(self):
'''
Return the cost as shares*price
'''
return self.shares * self.price
def sell(self, nshares):
'''
Sell a number of shares and return the remaining number.
'''
self.shares -= nshares