以元组的形式传递多个参数

O.r*_*rka 3 python arguments tuples class object

我正在传递大量数据;具体来说,我试图将函数的输出传递给一个类,并且输出包含一个具有三个变量的元组。我不能像在输入参数中那样直接将我的函数(元组)的输出传递到类中。

如何格式化元组,使其在没有 的情况下被班级接受input_tuple[0], input_tuple[1], input_tuple[2]

这是一个简单的例子:

#!/usr/bin/python

class InputStuff(object):

    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c


input_tuple = (1, 2, 3)
instance_1 = InputStuff(input_tuple)

# Traceback (most recent call last):
#   File "Untitled 3.py", line 7, in <module>
#     instance_1 = InputStuff(input_tuple)
# TypeError: __init__() takes exactly 4 arguments (2 given)

InputStuff(1, 2, 3)
# This works
Run Code Online (Sandbox Code Playgroud)

Mur*_*nik 5

您可以使用*运算符来解包参数列表

input_tuple = (1,2,3)
instance_1 = InputStuff(*input_tuple)
Run Code Online (Sandbox Code Playgroud)