Skip to main content
 首页 » 编程设计

python之以元组形式传递多个参数

2025年05月04日34kenshinobiy

<分区>

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

如果没有 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