简体   繁体   中英

Passing list as normal arguments to function in python

Say I have some code like this:

d = {'range': range}
args = [10]
d['range'](args)

args will be of variable content and length. I can handle any exceptions this would throw... I can't change the function I'm calling (range is builtin), and it would be preferable to not wrap it. How can I make this code function properly?

The functionality that you are looking for is called argument unpacking :

>>> d = {'range': range}
>>> args = [10]
>>> d['range'](*args)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

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