0

我将一个 python 模块作为PyObject. 我想检查这个值是否在我的 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)
      ^~~
4

1 回答 1

1

PyObject_IsTrue似乎做你想做的事

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.
于 2015-07-09T22:17:19.720 回答