【发布时间】:2015-04-13 10:09:31
【问题描述】:
我需要将 Python 中函数的输出作为参数传递给另一个函数。唯一的问题是第一个函数返回一个元组,整个元组需要传递给第二个函数。
例如:
我有一个库函数:
def some_function(some_string, some_number):
# does something
return (text,id)
现在我想将从some_function 返回的text 和id 作为参数传递给另一个函数。问题是该函数还有其他参数。我还需要检索由some_string 和number 的不同值生成的许多文本和ID。
根据满足的条件,我想调用另一个看起来像这样的函数
if abcd:
other_function(name,phone,**some_function("abcd","123")**,age)
elif xyz:
other_function(name,phone,**some_function("xyz","911")**,age)
else:
other_function(name,phone,**some_function("aaaa","000")**,age)
那么我应该用什么替换 some_function("abcd","123") 以便它扩展元组并将 text 和 id 作为参数发送?
other_function 是这样定义的
def other_function(name, phone, text, id, age):
...
return
简单地将some_function("aaaa","000") 作为参数传递是否有效?
我问这个是因为我写了一个简单的代码来测试我的假设,它给了我一个空白的输出。
def f(id,string):
return ("123", "hello")
def a(name, age, id, words, phone):
print name + age + id + words + phone
def main():
a("piyush", "23", f("12", "abc"), "123")
【问题讨论】:
-
这不会给出空白输出,它会引发 TypeError。
-
@DanielRoseman per the OP's link below,看起来他们实际上并没有运行任何东西!你是对的,通过;
a() takes exactly 5 arguments (4 given).
标签: python argument-passing function-calls