Python check if isinstance any type in list?

后端 未结 4 1214
北荒
北荒 2021-02-02 05:05

How do I pythonicly do:

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


        
4条回答
  •  情话喂你
    2021-02-02 05:39

    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])
    

提交回复
热议问题