以元组形式传递多个参数 [英] Pass multiple arguments in form of tuple

查看:202
本文介绍了以元组形式传递多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

您可以使用*运算符 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆