Python 3 isinstance

  • Thread starter Lambert, David W (S&T)
  • Start date
L

Lambert, David W (S&T)

Please, why isn't a set permitted as the second argument to isinstance?
 
C

Carl Banks

Please, why isn't a set permitted as the second argument to isinstance?

The Python development team might be open to adding it if you were to
submit a patch. The use case is fairly obvious. I doubt any of them
would respond to a wishlist request, though.

As a workaround, you can just cast the set to a tuple like this:

isinstance(x,tuple(s))


Carl Banks
 
R

Rhamphoryncus

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top