i a*_*eph 2 python tuples class
我有一个返回元组的函数
x = (1, 2, 3, 4, 5, 6, 7, 8, 9)
Run Code Online (Sandbox Code Playgroud)
我也有一个类需要10个args(包括self)
我希望元组能够填充类中的args,但如果我只是放
y = Class(x)
Run Code Online (Sandbox Code Playgroud)
它返回错误
> TypeError: __init__() takes exactly 10 arguments (2 given)
Run Code Online (Sandbox Code Playgroud)
我知道可以使用
y = Class(x[0], x[1], ... x[8])
Run Code Online (Sandbox Code Playgroud)
但这似乎非常冗长.有没有更好的方法呢?
你需要解压缩它:
>>> def foo(a, b, c, d, e, f, g, h, i, j):
... return a
...
>>> x = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>> foo(*x)
1
Run Code Online (Sandbox Code Playgroud)