0

How can you initialize a class by calling a constructor with a list?

class ETF:
    def __init__(self, symbol, name, asset_class):
        self.symbol = symbol
        self.name = name
        self.asset_class = asset_class 
this_list = ['qqq','q invests', 'equity']

thisETF = ETF(this_list)

The code above will result in an error message because it only counts the list as one argument:

TypeError: __init__() missing 2 required positional arguments: 'name', 'asset_class'
FinDev
  • 429
  • 5
  • 16

1 Answers1

3

Just unpack the list. It will then pass the list elements to the constructor instead of the list.

thisETF = ETF(*this_list)
darcamo
  • 3,294
  • 1
  • 16
  • 27