Question about isinstance()

Rene Pijlman reply.in.the.newsgroup at my.address.is.invalid
Fri Jan 27 04:55:24 EST 2006


Steven D'Aprano:
>Rene Pijlman:
>> Mr.Rech:
>>>   def __eq__(self, other):
>>>      try:
>>>         return self.an_attribute == other.an_attribute
>>>      except AttributeError:
>>>         return False
>> 
>> This may give unexpected results when you compare a foo with an instance
>> of a completely different type that happens to have an attribute called
>> 'an_attribute'.
>
>That's a trade-off, isn't it?
>
>On the one hand, you risk false negatives, by refusing to compare against
>things you didn't think of. 

Well no, when comparing against things you didn't think of the __eq__
shouldn't return a false False, it should return NotImplemented. After
all, the things you didn't think of are not (yet) implemented.

"A rich comparison method may return NotImplemented if it does not
implement the operation for a given pair of arguments."
http://www.python.org/doc/ref/customization.html

-- 
René Pijlman

Wat wil jij worden?  http://www.carrieretijger.nl



More information about the Python-list mailing list