forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstock.py
More file actions
25 lines (23 loc) · 728 Bytes
/
Copy pathstock.py
File metadata and controls
25 lines (23 loc) · 728 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
'''
Exercise 4.1: Objects as Data Structures
Exercise 4.2: Adding some Methods
'''
class Stock:
def __init__(self,name,shares,price):
self.name = name
self.shares = shares
self.price = price
def cost(self):
return self.shares* self.price
def sell(self,quantity):
self.shares -= quantity
def buy(self,quantity,price):
self.price = (quantity*price + self.shares*self.price)/(quantity+self.shares)
self.shares +=quantity
# using inheritance
class MyStock (Stock):
def __init__(self,name,shares,price,factor):
super().__init__(name,shares,price)
self.factor = factor
def cost(self):
return self.factor* super().cost()