To check whether a variable is None or not, use the is operator in Python. With the is operator, use the syntax object is None to return True if the object has the type NoneType and False otherwise.
Since None is a singleton, testing for object identity (using == in C) is sufficient.
Use the is not operator to check if a variable is not None in Python, e.g. if my_var is not None: . The is not operator returns True if the values on the left-hand and right-hand sides don't point to the same object (same location in memory).
As the null in Python, None is not defined to be 0 or any other value. In Python, None is an object and a first-class citizen!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With