How to compare inheritance with several classes?
python
Solution
You can pass a tuple of classes as 2nd argument to isinstance.
>>> isinstance(u'hello', (basestring, str, unicode))
True
Looking up the docstring would have also told you that though ;)
>>> help(isinstance)
Help on built-in function isinstance in module __builtin__:
isinstance(...)
isinstance(object, class-or-type-or-tuple) -> bool
Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).
Problem
I want to check if an object is an instance of any class in a list/group of Classes, but I can't find if there is even a pythonic way of doing so without doing ``` if isinstance(obj, Class1) or isinstance(obj, Class2) ... or isinstance(obj, ClassN): # proceed with some logic ``` I mean, comparing class by class. It would be more likely to use some function similar to `isinstance` that would receive n number of Classes to compare against if that even exists. Thanks in advance for your help!! :)