Test None for an object that does not implement ==
Nobody
nobody at nowhere.com
Sun Dec 25 04:38:07 EST 2011
On Sat, 24 Dec 2011 23:09:50 -0800, GZ wrote:
> I run into a weird problem. I have a piece of code that looks like the
> following:
>
> f(...., a=None, c=None):
> assert (a==None)==(c==None)
>
>
> The problem is that == is not implemented sometimes for values in a
> and c, causing an exception NotImplementedError.
I have no idea how that can happen. If a.__eq__(None) returns
NotImplemented, the interpreter should flip the test and perform the
equivalent of None.__eq__(a), which will return False.
> So how do I reliably test if a value is None or not?
As Paul says, use "is" to check whether a value _is_ None. Checking for
equality is almost certainly the wrong thing to do; 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.
More information about the Python-list
mailing list