Test None for an object that does not implement ==

Ian Kelly ian.g.kelly at gmail.com
Sun Dec 25 21:02:16 EST 2011


On Sun, Dec 25, 2011 at 2:38 AM, Nobody <nobody at nowhere.com> wrote:
> 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.

Maybe the class has a misbehaved __eq__ that raises
NotImplementedError directly instead of returning NotImplemented.



More information about the Python-list mailing list