简体   繁体   English

Python:函数和参数列表...可能吗?

[英]Python: Function and Argument list… is it possible?

I am writing some code that receives functions with variable numbers of arguments. 我正在编写一些代码,以接收带有可变数量参数的函数。 I also have lists of those arguments (but the functions expect separate arguments, not one list). 我也有这些参数的列表(但是函数需要单独的参数,而不是一个列表)。 Is there a way to convert this list into arguments of a form that the functions might like (and sadly, they cannot just be converted to strings)? 有没有一种方法可以将该列表转换为函数可能喜欢的形式的参数(可悲的是,它们不能仅转换为字符串)?

Yes, just prefix the list with a * to unpack it: 是的,只需在列表前面加上*即可将其解压缩:

args = [1, 2, 3]
foo(*args) # equivalent to foo(1, 2, 3)

More details available at the Python tutorial . 有关更多详细信息请参见Python教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM