Test None for an object that does not implement ==

Lie Ryan lie.1296 at gmail.com
Sun Dec 25 06:10:28 EST 2011


On 12/25/2011 08:38 PM, Nobody wrote:

> nothing should compare equal to None except for None itself, so "x is None"
 > and "x == None" shouldn't produce different results unless there's a
 > bug in the comparison method.

not necessarily, for example:

import random
class OddClass:
     def __eq__(self, other):
         return [True, False][random.randint(0, 1)]

x = OddClass()
print x == None
print x == None
print x == None
print x == None
print x == None


Now, whether doing something like that is advisable or not, that's a 
different question; however nothing in python states that you couldn't 
have something that compare equal to None whether there is a bug or not 
in the comparison method.




More information about the Python-list mailing list