Question about isinstance()

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri Jan 27 17:47:06 EST 2006


In <c2rjt15o8bsg7h6114jf45tf6r680058om at 4ax.com>, Rene Pijlman wrote:

> 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.

I think Steven thinks that it is possible that you compare to an object of
a different type which has the same attributes as expected by the
`__eq__()` method.  If the first test is `isinstance()` for the "correct"
type you rule out those cases and give a false `False`.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list