【发布时间】:2015-12-30 01:02:08
【问题描述】:
我正在传递大量数据;具体来说,我试图将函数的输出传递给一个类,并且输出包含一个带有三个变量的元组。我不能像输入参数一样将函数(元组)的输出直接传递到类中。
如何格式化元组以便在没有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
【问题讨论】:
-
instance_1 = InputStuff(*input_tuple)- 见stackoverflow.com/q/36901/3001761 -
那行得通,你能把它作为答案让我把它标记为正确吗?
-
我已经告诉过你副本是什么 - 我会关闭它,但我的选票已经用完了! (感谢@BhargavRao)
-
@Jonrsharpe 我看到它可能是重复的,但如果有人不知道 * 运算符或者用 * 解包参数列表是可能的,那么他们不会搜索它来找到回答。我认为标题的措辞在搜索中非常直接。
-
你的意思是像stackoverflow.com/q/1993727/3001761, stackoverflow.com/q/10625220/3001761, ...?您认为我们需要多少个不同的版本?
标签: python class object arguments tuples