Check if PyObject is None

本小妞迷上赌 提交于 2019-12-20 17:34:09

问题


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.

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

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.


回答1:


You can just compare directly with Py_None using ==:

if (obj == Py_None)

From the docs:

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.



来源:https://stackoverflow.com/questions/29732838/check-if-pyobject-is-none

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!