Python将*args转换为列表

Dan*_*son 9 python arrays args python-2.7

这就是我要找的东西:

def __init__(self, *args):
  list_of_args = #magic
  Parent.__init__(self, list_of_args)
Run Code Online (Sandbox Code Playgroud)

我需要将*args传递给单个数组,以便:

MyClass.__init__(a, b, c) == Parent.__init__([a, b, c])
Run Code Online (Sandbox Code Playgroud)

And*_*ark 15

没什么太神奇了:

def __init__(self, *args):
  Parent.__init__(self, list(args))
Run Code Online (Sandbox Code Playgroud)

在里面__init__,变量args只是一个传入任何参数的元组.实际上你可以使用Parent.__init__(self, args)它,除非你真的需要它作为一个列表.

作为旁注,使用super()是优选的Parent.__init__().


Sim*_*mon 11

我在 senddex 教程中找到了一段处理此问题的代码:

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

尝试这个:

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

test_args('Sun','Rain','Storm','Wind')
Run Code Online (Sandbox Code Playgroud)

结果:

[‘太阳’、‘雨’、‘暴风雨’、‘风’]