26

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])
Daniel Watson
  • 463
  • 1
  • 4
  • 9

3 Answers3

32

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__().

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
10

There is this piece of code that I picked up in sentdex tutorials that deals with this:

https://www.youtube.com/watch?v=zPp80YM2v7k&index=11&list=PLQVvvaa0QuDcOdF96TBtRtuQksErCEBYZ

Try this:

def test_args(*args):
    lists = [item for item in args]
    print lists

test_args('Sun','Rain','Storm','Wind')

Result:

['Sun', 'Rain', 'Storm', 'Wind']

Simon
  • 19,658
  • 27
  • 149
  • 217
0

If you're looking for something in the same direction as @simon 's solution, then:

def test_args(*args): lists = [*args] print(lists) test_args([7],'eight',[[9]])

Result:

[[7], 'eight', [[9]]]

arunkumaraqm
  • 337
  • 1
  • 13