简体   繁体   中英

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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