如何将参数传递给具有变量参数的python函数?

2024-05-21 02:11:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Boosi::Python来回调C++侧的Python函数,Python函数可能有不同的参数,看起来像:

foo(int) ,
foo(int ,double),
foo(string , othertype,...) , 
< P>在我的C++程序中,我将Foo发送为升压::Python::对象,

void Call(object foo)
{
//check each argument and assign value for example
int arg1=2;
string arg2="hehe";
.....
//then send above arguments to foo , but how to send?
foo(??);
}

现在我可以得到每个参数的名称并相应地给出值,但我的问题是:如何将值传递给foo

我使用boost::python::list或tuple收集数据,但不起作用,参数错误。 有人能帮我吗?多谢各位


Tags: to对象函数程序send参数stringfoo
1条回答
网友
1楼 · 发布于 2024-05-21 02:11:41
def myFun(arg1, **kwargs):  
    for key, value in kwargs.items(): 
        print ("%s == %s" %(key, value)) 

# Driver code 
myFun("Hi", first ='Geeks', mid ='for', last='Geeks') 

您可以使用**参数: 因此,您可以将不同类型的多个参数作为dict类型传递

有关详细信息,请查看此链接:https://www.geeksforgeeks.org/args-kwargs-python/

相关问题 更多 >