我正在将python模块作为PyObject
传递给C。我想使用以下形式检查我的C代码中此值是否为NONE:
int func(PyObject tmp)
{
if(tmp)
{
// etc
我收到以下错误。如何从PyObject转换为布尔值,与Python的if函数的行为类似。值得注意的是,当
tmp
是boost::python::object
变量时,此命令将按预期运行。ex_program.cpp:72:7: error: value of type 'PyObject' (aka '_object') is not contextually convertible to 'bool'
if (tmp)
^~~
最佳答案
PyObject_IsTrue
seems to do what you want:
int PyObject_IsTrue(PyObject *o)
Returns 1 if the object o is considered to be true, and 0 otherwise. This is equivalent to the Python expression not not o. On failure, return -1.
关于python - 错误:类型为“PyObject”(也称为“_object”)的值在上下文中无法转换为“ bool ”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31329062/