[Tutor] pychecker: x is None or x == None
Alan G
alan.gauld at freenet.co.uk
Sat Aug 13 12:50:40 CEST 2005
> if x == None:
> do_something()
>
> but then someone thought that we should really change these to
>
> if x is None:
> do_something()
>
> However. if you run pychecker on these two snippets of code, it
> complains about the second, and not the first:
Personally I'd use
if not x:
do_something()
No idea what pyChecker thinks of that though! :-)
And of course it's slightly different to a specific check for None,
it will catch 0, [], '',"", etc...
Of the two forms you suggest I'd stick with equality since the
identity test (is) assumes that only one instance of None ever
exists which could potentially change in some future exotic
version of Python... You really are interested in the value
of x not its identity.
Alan G.
More information about the Tutor
mailing list