[C++-sig] Checking for or null PyObject* or PyNone

Stefan Seefeld seefeld at sympatico.ca
Mon Feb 8 15:46:43 CET 2010


On 02/08/2010 02:02 AM, Ralf W. Grosse-Kunstleve wrote:
>> if (obj) ...
>>      
>    
>> is definitely the right way to do it.
>>      
> Sorry to correct, but it isn't equivalent to "obj is None".
> if (obj) uses PyObject_IsTrue(); see
> boost/python/object_operators.hpp line 60.
>    

Oh, thanks for the correction !

I guess PyObject_IsTrue() returns false for None, so it may just have 
worked by coincidence in the past. But yes, there is a need to 
disambiguate between Py_False and Py_None.

> I'll add
>
>    obj.is_none()
>
> and
>
>    obj.is_not_none()
>
> when I get a chance.
>    

Thanks. I don't think there is a need for more than one addition, 
though. I.e., "obj.is_not_none()" is redundant, and could be spelled 
simply "!obj.is_none()". (Or in fact, "obj.is_none()" may just be the 
same as "obj == object()", though the latter is a little wasteful.

Thanks,
         Stefan

-- 

       ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list