Source From Here
Question
Is there a way to expand a Python tuple into a function - as actual parameters? For example, here expand() does the magic:
How-To
myfun(*tuple) does exactly what you request. This is actually very simple to do in Python, simply loop over the list and use the splat operator (*) to unpack the tuple as arguments for the function:
Supplement
* [Quick Python] 9. Functions
* The Python Tutorial - More Control Flow Tools - Unpacking Argument Lists
Question
Is there a way to expand a Python tuple into a function - as actual parameters? For example, here expand() does the magic:
- tuple = (1, "foo", "bar")
- def myfun(number, str1, str2):
- return (number * 2, str1 + str2, str2 + str1)
- myfun(expand(tuple)) # (2, "foobar", "barfoo")
myfun(*tuple) does exactly what you request. This is actually very simple to do in Python, simply loop over the list and use the splat operator (*) to unpack the tuple as arguments for the function:
Supplement
* [Quick Python] 9. Functions
* The Python Tutorial - More Control Flow Tools - Unpacking Argument Lists
沒有留言:
張貼留言