Python 3 isinstance

Rhamphoryncus rhamph at gmail.com
Thu Jan 15 21:01:25 EST 2009


On Jan 14, 3:14 pm, "Lambert, David W (S&T)" <Lamber... at Corning.com>
wrote:
> Please, why isn't a set permitted as the second argument to isinstance?

The real problem is it would be misleading.  isinstance(a, myset)
suggests implementation like type(a) in myset, but it's really any
(isinstance(a, t) for t in myset).

If myset is arbitrarily large (as the examples suggest), and you're
checking on every call, the code would be so slow as to be considered
broken.  This invalidates the use case.

The abc module in 2.6/3.0 is a possible alternative.  Create an ABC
type, register each type in myset as part of it, then do isinstance(a,
MyABC).  Internally the ABC will cache lookups, effectively getting
you back to O(1) cost.



More information about the Python-list mailing list