171

How do I compactly perform the following:

var = 7.0
var_is_good = (
    isinstance(var, classinfo1) or
    isinstance(var, classinfo2) or
    isinstance(var, classinfo3)
)
0

5 Answers 5

365

isinstance() takes a tuple of classes for the second argument. It'll return true if the first argument is an instance of any of the types in that sequence:

isinstance(var, (classinfo1, classinfo2, classinfo3))

In other words, isinstance() already offers this functionality, out of the box.

From the isinstance() documentation:

If classinfo is neither a class object nor a type object, it may be a tuple of class or type objects, or may recursively contain other such tuples (other sequence types are not accepted).

Emphasis mine; note the recursive nature; (classinfo1, (classinfo2, classinfo3)) is also a valid option.

0
34

From Python 3.10 you can use the new New Type Union Operator, e.g. :

isinstance(var, classinfo1 | classinfo2)

See PEP 604 for details.

18

You were pretty close with the title of your question already. You could use any and a list:

var = 7.0
var_is_good = any([isinstance(var, classinfo1),
                   isinstance(var, classinfo2),
                   isinstance(var, classinfo3), ...
                   isinstance(var, classinfoN)])

But looking in the docs of isinstance reveals:

Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. If object is not an object of the given type, the function always returns false. If classinfo is not a class (type object), it may be a tuple of type objects, or may recursively contain other such tuples (other sequence types are not accepted). If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised.

This means the better way to do it is

var = 7.0
var_is_good = isinstance(var, (classinfo1,
                               classinfo2,
                               classinfo3,
                               ...,
                               classinfoN))
5
  • 3
    Repeating isinstance with only the classinfo changing doesn't seem very Pythonic to me.
    – Makoto
    Oct 23, 2015 at 21:03
  • 1
    Try var.__class__. Is that what you want? @Makoto: I agree. Not Pythonic at all!
    – flamenco
    Oct 23, 2015 at 21:04
  • @Makoto Yup. But that was something D Adams could probably have easily found by searching the docs for "any". I've expanded my answer to the better version ... but obviously Martijn Pieters was faster ... once again. Oct 23, 2015 at 21:06
  • 4
    any() with a list is.. pointless. Use a generator expression if you must use any(), then at least you get the benefit of the early exit.
    – Martijn Pieters
    Oct 23, 2015 at 21:11
  • 1
    I think the isinstance with tuple is the way to go. I just wanted to mention the solution with any as it seemed to be what he is asking for in the title. Oct 23, 2015 at 21:13
8

This will solve your problem:

valid_instance_types = <tuple of types you want to allow>
var_is_good = isinstance(var, valid_instance_types)

Based on the documentation there are a lot of ways you can pass values of types in to isinstance.

You might also look into (unfortunately named) voluptuous if you're trying to do a more complicated validation of which this is just a part.

0
0

You generally shouldn't be using isinstance, but what you're wanting to do can be accomplished with the any() builtin function.

var_is_good = any(isinstance(var, t) for t in [type1, type2, type3])
6
  • 13
    This is overkill; isinstance() offers the functionality out of the box without using any().
    – Martijn Pieters
    Oct 23, 2015 at 21:02
  • @MartijnPieters I didn't realize that isinstance can take multiple types, but I feel like the answer is still a good one because the question can be taken more broadly to be "How do I check if any of the return values from a particular function with different params is true"
    – Chad S.
    Oct 23, 2015 at 21:19
  • Whether or not you should use isinstance() is irrelevant.
    – RFV
    Mar 22, 2020 at 5:56
  • @ChadS. I would like to ask you why do you think he shouldn't use isinstance()? Thank you.
    – idature
    Oct 23, 2020 at 14:43
  • Because python eschews type checking. It's a lot of extra code (and CPU cycles) for little benefit. Duck typing is preferred.
    – Chad S.
    Oct 28, 2020 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.