gpt4 book ai didi

python - 错误: value of type 'PyObject' (aka '_object' ) is not contextually convertible to 'bool'

转载 作者:行者123 更新时间:2023-11-30 15:18:36 24 4
gpt4 key购买 nike

我将 python 模块作为 PyObject 传递给 C。我想使用以下形式检查该值在我的 C 代码中是否为 NONE:

int func(PyObject tmp)
{
if(tmp)
{
// etc

我收到以下错误。如何从 PyObject 转换为 bool 值,类似于 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 - 错误: value of type 'PyObject' (aka '_object' ) is not contextually convertible to 'bool' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31329062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com