comparison with None
Steven Howe
howe.steven at gmail.com
Thu Apr 19 12:03:10 EDT 2007
Steven D'Aprano wrote:
> On Thu, 19 Apr 2007 08:18:30 -0400, Steve Holden wrote:
>
>
>>> Which is why I suggested using the explicit type(x) == types.NoneType as
>>> opposed to
>>> x is None
>>>
>>>
>>>
>> This seems to go entirely against the spirit of the language. It's about
>> as sensible as writing
>>
>> (3 > 4) == True
>>
>
>
> Please! For extra certainty, you should write that as:
>
> ((int(3) > int(4)) == True) == True
>
> Explicit is better than sensible, yes?
>
> *wink*
>
>
>
>
Your example, even with the *wink*, is stupid. The language requires 3
to be an integer, 4 to be an integer.
The point I was show is with respect to a returned variable (like from a
function or method? *wink* *wink*).
For example, if you expect an open file handle, but get a NoneType
because you didn't really open a file (having given a bad name or maybe
didn't have permission to open a file), then it would be best to test
the type of return object before using it. Then you program could handle
the error gracefully (*wink* *wink* *wink*).
As much as I love Python, it's ability to morph an object type can be a
pain. Testing before using can prevent a program from Error-ing out.
Steven Howe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070419/240131e0/attachment.html>
More information about the Python-list
mailing list