Permanent objects?

Erik Max Francis max at alcyone.com
Wed Dec 25 16:46:10 EST 2002


Kevin Altis wrote:

> Hmm, I think I tend to use
> 
>   if x == None:
> and
>   if x != None:
> 
> rather than
> 
>   if x is None:
> and
>   if not x is None:
> 
> I don't suppose it really matters or that one is better or clearer
> than the
> other?

If everybody plays together well, it shouldn't make a difference, but
someone perverse could create a custom instance that overrides either
__eq__ or __cmp__ so that tests equal to None.  I can't think of a case
when it would appropriate for someone to do this, but someone _could_.

The canonical test is "x is None" or "x is not None" (or "not x is
None," obviously), since there you're testing by identity rather than
equality.  I generally recommend that people use the canonical pattern,
since there's really no striking reason not to (there's no benefit in
using == over is).  One should also use is, not ==, for testing types
(in the rare case when that actually is warranted, of course).

Then again, someone _really_ perverse could rebind None in the first
place, leaving you up the creek.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ My land's only borders lie / Around my heart
\__/ The Russian, _Chess_
    Sade Deluxe / http://www.sadedeluxe.com/
 The ultimate Sade encyclopedia.



More information about the Python-list mailing list