简体   繁体   English

如何将元组作为 function 的参数传递

[英]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.我想调用第一个参数中包含的 function,第二个参数中的 args 加上其他参数。 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. * 提取(扩展)内部元组,并将它们作为 arguments 传递给 function。

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

If you want to add the origin to the args, something like this should work:如果你想将原点添加到 args,这样的事情应该可以工作:

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

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

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