检查PyObject是否为None [英] Check if PyObject is None

查看:610
本文介绍了检查PyObject是否为None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想检查一下 PyObject 是否为 None 。我天真地期望从函数返回的任何 Pyobject * 将是一个NULL指针,但是似乎

I would just like to check if a PyObject that I have is None. I naively expected that any None Pyobject * returned from a function would be a NULL pointer, but that doesn't seem to be the case.

所以:如何检查我的 PyObject * 是否指向无对象?

So: how do I check if a PyObject * of mine points to a None object?

我知道有像 PyInt_Check(PyObject *),但我找不到像 PyNone_Check 。我想我可以检查我的 PyObject Py_None 之间的平等,但结果是我甚至不知道

I know that there are macros like PyInt_Check(PyObject *) around, but I couldn't find anything like PyNone_Check. I thought I could just check the equality between my PyObject and Py_None, but turns out I don't even know how to make equality comparisons with this library.

推荐答案

您可以直接与 Py_None 使用 ==

if (obj == Py_None)

docs


注意 PyTypeObject 不直接暴露在
Python / C API中。 由于 None 是一个单例,测试对象标识
(使用C中的 ==
对于
没有 PyNone_Check()函数同样的原因。

Note that the PyTypeObject for None is not directly exposed in the Python/C API. Since None is a singleton, testing for object identity (using == in C) is sufficient. There is no PyNone_Check() function for the same reason.

这篇关于检查PyObject是否为None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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