Question about isinstance()

Steven D'Aprano steve at REMOVETHIScyber.com.au
Thu Jan 26 23:34:17 CET 2006


On Thu, 26 Jan 2006 19:04:13 +0100, Rene Pijlman wrote:

> Mr.Rech:
>>Now, avoiding isinstace() I've written the following code:
>>
>>class foo(object):
>>   ...
>>   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. On the other hand, you risk false positives,
by comparing against more generic objects.

I guess that trade-off is one each programmer must decide for herself, in
full understanding of the pros and cons of each method.


-- 
Steven.




More information about the Python-list mailing list