[Doc-SIG] Does the "is" operator only matter for mutable object?
Nick Coghlan
ncoghlan at gmail.com
Mon Mar 7 01:30:08 CET 2011
On Mon, Mar 7, 2011 at 9:08 AM, Laura Creighton <lac at openend.se> wrote:
> Singleton objects such as ``True``, ``False``, and ``None`` are always
> the same object. The canonical way to test whether an object is
> a singleton is to test for object identity, not equality. So
>
>>>> if x is None:
> ... do something ...
>
> not
>
>>>> if x == None:
> ... do something ...
Careful with that - "if x is True:" and "if x is False:" (along with
s/is/==/) are almost always the wrong thing to do, but a reader could
easily generalise the above to cover those two cases as well.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Doc-SIG
mailing list