Python converting *args to list

Daniel Watson picture Daniel Watson · Mar 19, 2013 · Viewed 24.3k times · Source

This is what I'm looking for:

def __init__(self, *args):
  list_of_args = #magic
  Parent.__init__(self, list_of_args)

I need to pass *args to a single array, so that:

MyClass.__init__(a, b, c) == Parent.__init__([a, b, c])

Answer

Andrew Clark picture Andrew Clark · Mar 19, 2013

Nothing too magic:

def __init__(self, *args):
  Parent.__init__(self, list(args))

Inside of __init__, the variable args is just a tuple with any arguments that were passed in. In fact you can probably just use Parent.__init__(self, args) unless you really need it to be a list.

As a side note, using super() is preferable to Parent.__init__().