在boost::python中从kwargs提取参数 我有一个C++类,我用Boosi::Python构建了一个Python模块。我有几个函数,我想采用关键字参数。我已经设置了包装器函数来传递给原始参数,这很好,但是我想为函数参数构建一些错误检查。有没有标准的方法可以做到这一点 我的函数原型,在C++中,看起来有点像: double MyClass::myFunction(int a, int b, int c);
第三个参数是可选的,默认值为0(到目前为止,我已经在boost::python中使用宏实现了这一点)。在python中,我希望能够实现以下行为:在boost::python中从kwargs提取参数 我有一个C++类,我用Boosi::Python构建了一个Python模块。我有几个函数,我想采用关键字参数。我已经设置了包装器函数来传递给原始参数,这很好,但是我想为函数参数构建一些错误检查。有没有标准的方法可以做到这一点 我的函数原型,在C++中,看起来有点像: double MyClass::myFunction(int a, int b, int c);,c++,python,boost-python,keyword-argument,C++,Python,Boost Python,Keyword Argument,第三个参数是可选的,默认值为0(到目前为止,我已经在boost::python中使用宏实现了这一点)。在python中,我希望能够实现以下行为: MyClass.my_function(1) # Raises exception MyClass.my_function(2, 3) # So that a = 2, b = 3 and c defaults to 0 MyClass.my_function(2, 3, 1) # As above, but now c = 1 MyClass.my_
MyClass.my_function(1) # Raises exception
MyClass.my_function(2, 3) # So that a = 2, b = 3 and c defaults to 0
MyClass.my_function(2, 3, 1) # As above, but now c = 1
MyClass.my_function(2, 3, 1, 3) # Raises exception
MyClass.my_function(3, 1, c = 2) # So a = 3, b = 1 and c = 2
MyClass.my_function(a = 2, b = 2, c = 3) # Speaks for itself
MyClass.my_function(b = 2, c = 1) # Raises exception
在boost::python或raw_函数包装器中是否有什么东西可以帮助实现这一点,或者我是否需要自己编写代码来检查所有这些?如果需要,我如何提出例外情况?有标准的方法吗?该文件提供了一系列用于指定参数关键字的类。特别是,Boost.Python提供了一个表示潜在关键字参数的类型。它重载逗号运算符,以允许对参数列表进行更自然的定义
将MyClass
上的myFunction
显示为myu函数
,其中a
、b
和c
是关键字参数,c
的默认值为0
,可以编写如下:
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::class_<MyClass>("MyClass")
.def("my_function", &MyClass::myFunction,
(python::arg("a"), "b", python::arg("c")=0))
;
}
BOOST\u PYTHON\u模块(示例)
{
名称空间python=boost::python;
python::类(MyClass)
.def(“my_函数”,&MyClass::myFunction,
(python::arg(“a”),“b”,python::arg(“c”)=0)
;
}
下面是一个完整的示例:
#include <boost/python.hpp>
class MyClass
{
public:
double myFunction(int a, int b, int c)
{
return a + b + c;
}
};
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::class_<MyClass>("MyClass")
.def("my_function", &MyClass::myFunction,
(python::arg("a"), "b", python::arg("c")=0))
;
}
#包括
类MyClass
{
公众:
双myFunction(int a、int b、int c)
{
返回a+b+c;
}
};
BOOST_PYTHON_模块(示例)
{
名称空间python=boost::python;
python::类(MyClass)
.def(“my_函数”,&MyClass::myFunction,
(python::arg(“a”),“b”,python::arg(“c”)=0)
;
}
互动使用:
>>导入示例
>>>my_class=example.MyClass()
>>>my_类。my_函数(1)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ArgumentError:中的Python参数类型
MyClass.my_函数(MyClass,int)
与C++签名不匹配:
my_函数(MyClass{lvalue},int a,int b,int c=0)
>>>断言(5==my_类。my_函数(2,3))
>>>断言(6==my_类。my_函数(2,3,1))
>>>my_类。my_函数(2,3,1,3)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ArgumentError:中的Python参数类型
MyClass.my_函数(MyClass,int,int,int,int)
与C++签名不匹配:
my_函数(MyClass{lvalue},int a,int b,int c=0)
>>>断言(6==my_类。my_函数(3,1,c=2))
>>>断言(7==my_类。my_函数(a=2,b=2,c=3))
>>>my_类。my_函数(b=2,c=1)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ArgumentError:中的Python参数类型
MyClass.my_函数(MyClass)
与C++签名不匹配:
my_函数(MyClass{lvalue},int a,int b,int c=0)
这非常有效,我无意中在另一个函数上使用了类似的设置来重载该函数的参数,这有点令人尴尬。这也适用于施工人员吗?回答我自己的问题:是的。在包装器代码类声明中执行py::init((py::arg(“a”),py::arg(“b”)…)。(如果有人在这方面有困难,请注意围绕args的附加括号)在您的示例中,命名参数始终处于相同的“自然”顺序(a、b、c)。我想这也应该起作用:assert(6==my_class.my_函数(c=3,a=2,b=1)),对吗?