testing for valid reference: obj vs. None!=obs vs. obj is not None

Paul Rubin http
Tue Sep 5 00:29:56 EDT 2006


alf <ask at me> writes:
> I have a reference to certain objects. What is the most pythonic way
> to test for valid reference:

If you're intending to use None as a sentinel for an invalid reference,
then use
> 	if obj is not None:

You could also make a unique sentinel:

       Sentinel = object()
       ...
       if obj is not Sentinel:  ...

"if obj" isn't necessarily what you want; obj might be a valid but
empty container.

"if obj != None" is also wrong, for reasons someone else explained.



More information about the Python-list mailing list