[Python-ideas] Allow isinstance second argument to be a set of types

Mike Graham mikegraham at gmail.com
Thu Jul 7 19:54:00 CEST 2011


Any of the changes I see would make isinstance work. This basic sort
of API -- take a thing or a tuple-of-things (or often a list-of-things
or precariously an iterable-of-things) is a poor, inflexible one I
avoid in application code as I think do most Python programmers when
they can. The builtins and stdlib have this scattered about, but for
the most part it makes the relevant APIs less clear and simple.

I wonder if it isn't time to rethink isinstance at a more basic level.
The API SomeClass.isinstance(some_potential_instance) makes more sense
to me than isinstance(some_potential_instance, SomeClass), since it is
really an issue to do with the class. Indeed, recent Pythons are
implemented this way. It seems attractive with metaclasses to be able
to duck type this operation where your own metaclass which does not
inherit type can look just like an instance of type. If you wanted to
use an iterable of classes, you could use any(t.isinstance(o) for t in
types) or we could introduce a utility ABC factory ComposeTypes such
that we could do ComposeTypes(types).isinstance(o).

I do hesitate to think this change is good for a few reasons. It
introduces change and compatibility issues by introducing a new method
for all classes and deprecating a builtin. Additionally, it could seem
to encourage typechecking by improving it.


Mike



More information about the Python-ideas mailing list