错误:类型的PyObject“(又名”_object')的值不是上下文转换为“布尔” [英] error: value of type 'PyObject' (aka '_object') is not contextually convertible to 'bool'

查看:1187
本文介绍了错误:类型的PyObject“(又名”_object')的值不是上下文转换为“布尔”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递一个Python模块,以C作为的PyObject 。我要检查,看看这个值是无我的C code,使用这种形式:

  INT FUNC(TMP的PyObject)
{
   如果(TMP)
    {
     //等

我收到以下错误。如何从一个的PyObject转换为布尔值,呈三角的方式Python的IF函数的行为。值得注意的是,当 TMP 的boost ::蟒蛇::对象变量此命令按预期工作。

  ex_program.cpp:72:7:错误:类型的值的PyObject(又名_object')不是上下文转换为布尔
  如果(TMP)
      ^ ~~


解决方案

PyObject_IsTrue 似乎做你想要什么

  INT PyObject_IsTrue(*的PyObject O)    返回1,如果对象o被认为是真,否则为0。这相当于Python的前pression不是没有O操作。如果失败,返回-1。

I am passing a python module to C as a PyObject. I want to check to see if this value is NONE in my C code, using this form:

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

I am getting the following error. How can I convert from a PyObject to boolean value, simillar to the way Python's if function behaves. It is worth noting that when tmp is a boost::python::object variable this command works as expected.

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.

这篇关于错误:类型的PyObject“(又名”_object')的值不是上下文转换为“布尔”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆