0

This is the original code:

class stock(object):
def __init__(self,ticker):
    self.ticker = ticker

tickers = ['KO','JNJ','PFE','NKE','PG','WMT','MMM','IBM']
stocks = []
leng = len(tickers)
for i in tickers:
    stocks.append(stock(i))

This code creates a list of objects, each with attribute ticker corresponding to the elements in the list "tickers". However, is there anyway I can create a list of objects with object-name corresponding to the elements? In other words, I want to achieve the following codes through a loop:

KO = stock('KO')
JNJ = stock('JNJ')
...

But apparently the following loop is incorrect:

for i in tickers:
    i = stock(i)
ian_chan
  • 355
  • 5
  • 15

0 Answers0