简体   繁体   中英

How to pass tuple as parameters of a function

I have a tuple containing tuples.

EVENT_MAPPING = (
    (func1, (paramA1, paramA2, paramA3)),
    (func2, (paramB1)),
    (func3, (paramC1, paramC2)),
)

I iterate over the first tuple. And I want to call the function contained in the first param with args in the second param plus other param. For example, for the first tuple:

func1(origin, paramA1, paramA2, paramA3)

If their is no the "origin" param, I can have a call thanks to:

args[0](args[1])

But with the extra param ("origin"), I can't do things like that. I found a way to do it but it is heavy:

call = tuple(list(args[1]).insert(0,origin))
args[0](call)

Is there a better way to do this?

Thanks for your help

Like this, probably:

for func, args in EVENT_MAPPING:
  func(*args)

The * extracts (expand) the inner tuple and they are passed to the function as arguments.

for func, args in EVENT_MAPPING:
    func(origin, *args)

If you want to add the origin to the args, something like this should work:

for func, args in EVENT_MAPPING:
    func(*(origin,)+args)

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