检查PyObject是否为None [英] Check if PyObject is 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
forNone
is not directly exposed in the Python/C API. SinceNone
is a singleton, testing for object identity (using==
in C) is sufficient. There is noPyNone_Check()
function for the same reason.
这篇关于检查PyObject是否为None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!