Files
practical-python/Solutions/4_10/stock.py
David Beazley a31a9ee0f4 object patch
2020-05-28 17:52:19 -05:00

27 lines
584 B
Python

# stock.py
class Stock:
'''
An instance of a stock holding consisting of name, shares, and 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})'
def cost(self):
'''
Return the cost as shares*price
'''
return self.shares * self.price
def sell(self, nshares):
'''
Sell a number of shares
'''
self.shares -= nshares