MCPcopy Create free account
hub / github.com/dabeaz-course/practical-python / Stock

Class Stock

Solutions/7_11/stock.py:5–32  ·  view source on GitHub ↗

An instance of a stock holding consisting of name, shares, and price.

Source from the content-addressed store, hash-verified

3from typedproperty import String, Integer, Float
4
5class Stock:
6 '''
7 An instance of a stock holding consisting of name, shares, and price.
8 '''
9 name = String('name')
10 shares = Integer('shares')
11 price = Float('price')
12
13 def __init__(self,name, shares, price):
14 self.name = name
15 self.shares = shares
16 self.price = price
17
18 def __repr__(self):
19 return f'Stock({self.name!r}, {self.shares!r}, {self.price!r})'
20
21 @property
22 def cost(self):
23 '''
24 Return the cost as shares*price
25 '''
26 return self.shares * self.price
27
28 def sell(self, nshares):
29 '''
30 Sell a number of shares and return the remaining number.
31 '''
32 self.shares -= nshares

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected