简体   繁体   English

Python 检查列表中的 isinstance 是否为任何类型?

[英]Python check if isinstance any type in list?

How do I pythonicly do:我如何pythonicly做:

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

It seems silly I can't just pass in a list of classinfo's:我不能只传入一个 classinfo 列表,这似乎很愚蠢:

var_is_good = isinstanceofany( var, [classinfo1, classinfo2, ... , classinfoN] )

So what is the isinstanceofany function?那么isinstanceofany函数是什么?

isinstance() takes a tuple of classes for the second argument. isinstance()为第二个参数接受一个类元组 It'll return true if the first argument is an instance of any of the types in that sequence:如果第一个参数是该序列中任何类型的实例,它将返回 true:

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

In other words, isinstance() already offers this functionality, out of the box.换句话说, isinstance()已经提供了这个功能,开箱即用。

From the isinstance() documentation :isinstance()文档

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).如果classinfo既不是类对象也不是类型对象,则它可能是类或类型对象的元组,或者可能递归包含其他此类元组(不接受其他序列类型)。

Emphasis mine;强调我的; note the recursive nature;注意递归性质; (classinfo1, (classinfo2, classinfo3)) is also a valid option. (classinfo1, (classinfo2, classinfo3))也是一个有效的选项。

You were pretty close with the title of your question already.你已经很接近你的问题的标题了。 You could use any and a list:您可以使用any和一个列表:

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:但是查看isinstance的文档显示:

Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof.如果 object 参数是 classinfo 参数或其(直接、间接或虚拟)子类的实例,则返回 true。 If object is not an object of the given type, the function always returns false.如果 object 不是给定类型的对象,则该函数始终返回 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).如果 classinfo 不是类(类型对象),则它可能是类型 objects 的元组,或者可能递归包含其他此类元组(不接受其他序列类型)。 If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised.如果 classinfo 不是类型或类型元组和此类元组,则会引发 TypeError 异常。

This means the better way to do it is这意味着更好的方法是

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

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 .根据文档,有很多方法可以将类型的值传递给isinstance

You might also look into voluptuous if you're trying to do a more complicated validation of which this is just a part.如果您尝试进行更复杂的验证,这只是其中的一部分,您也可能会考虑voluptuous

From Python 3.10 you can use the new New Type Union Operator , eg :从 Python 3.10 开始,您可以使用新的New Type Union Operator ,例如:

isinstance(var, classinfo1 | classinfo2)

See PEP 604 for details.有关详细信息,请参阅PEP 604

您通常不应该使用isinstance ,但是您可以使用any()内置函数完成您想要做的事情。

var_is_good = any(isinstance(var, t) for t in [type1, type2, type3])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM