以元组的形式传递多个参数 [英] Pass multiple arguments in form of tuple
本文介绍了以元组的形式传递多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在传递大量数据;具体来说,我试图将函数的输出传递给一个类,并且输出包含一个具有三个变量的元组.我不能像在输入参数中那样直接将我的函数(元组)的输出传递到类中.
I'm passing a lot data around; specifically, I'm trying to pass the output of a function into a class and the output contains a tuple with three variables. I can't directly pass the output from my function (the tuple) into the class as in the input parameters.
如何格式化元组,使其在没有 input_tuple[0], input_tuple[1], input_tuple[2]
的情况下被类接受?
How can format the tuple so it is accepted by the class without 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
推荐答案
您可以使用 *
运算符来解压参数列表:
You can use the *
operator to unpack the argument list:
input_tuple = (1,2,3)
instance_1 = InputStuff(*input_tuple)
这篇关于以元组的形式传递多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文