In this code I'm trying to create a portfolio object that has a method that will generate a new instance of the class Stock
whenever a purchase is made. I would like that Stock
object to have its ticker as its name/pointer.
So I actually would like it to be interpreted as AMZN = Stock()
below, but I can't make this work. The output should be 3000
. I have tried different methods with no success so would be grateful for some advice. Quite new at this so might be some complications I'm not aware of...
class Stock:
def __init__(self, ticker, price, amount):
self.ticker = ticker
self.price = price
self.amount = amount
# Create portfolio to hold stocks and available funds
class Portfolio:
def __init__(self, funds):
self.funds = funds
self.stockPortfolio = []
# Buying a stock and adding it to the portfolio
def buyStock(self, ticker, price, amount):
#Add stock to portfolio
self.stockPortfolio.append(ticker)
ticker = Stock(ticker, price, amount) # Would like this to be read as AMZN = Stock()
return
p = Portfolio(100000)
p.buyStock("AMZN", 3000, 20)
print(AMZN.amount)