-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrintable_Representation_Object.py
More file actions
52 lines (46 loc) · 2.1 KB
/
Printable_Representation_Object.py
File metadata and controls
52 lines (46 loc) · 2.1 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# The methods __str__ and __repr__ will return a printable representation (rather than the object's address in memory) of an object associated with a class.
# The recommended approach is to utilize 'repr' to display a string that can be used to recreate the object.
class Stock:
def __init__(self, symbol: str, price: float, quantity: int):
"""
Initialize a Stock object with a symbol, price per share, and quantity of shares.
"""
self.symbol, self.price, self.quantity = symbol, price, quantity
def buy(self, amount: float):
"""
Buy a specified dollar amount of shares and update the quantity of shares.
"""
self.quantity += amount / self.price
def sell(self, amount: float):
"""
Sell a specified dollar amount of shares if enough shares are available.
"""
if self.quantity >= amount / self.price:
self.quantity -= amount / self.price
return True
else:
return False
def __str__(self): # Only accepts self, and should return a string
"""
Return a string representation of the Stock object.
"""
return f"{self.symbol}: {self.quantity} shares at ${self.price} per share"
def __repr__(self): # Only accepts self, and should return a string
"""
Return a string representation for creating a new Stock object.
"""
return f"Stock({self.symbol}, {self.price}, {self.quantity})"
# Example usage with Google's stock
google_stock = Stock("GOOG (NASDAQ)", 138.05, 20)
print(google_stock) # This will call the __str__ method
# GOOG (NASDAQ): 20 shares at $138.05 per share
# Buy more shares
google_stock.buy(5000)
print(repr(google_stock)) # This will call the __repr__ method
# Stock(GOOG (NASDAQ), 138.05, 56.21876131836291)
# Sell some shares
if google_stock.sell(10000):
print(f"Sold 10000 dollars worth of {google_stock.symbol} shares successfully.")
else:
print(f"Not enough shares to sell 10000 dollars worth of {google_stock.symbol} shares.")
# Not enough shares to sell 10000 dollars worth of GOOG (NASDAQ) shares.