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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33311258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:11:00  来源:igfitidea点击:

Python check if isinstance any type in list?

pythontypesisinstance

提问by D Adams

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 isinstanceofanyfunction?

那么isinstanceofany功能是什么呢?

采纳答案by Martijn Pieters

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

isinstance()为第二个参数接受一个类元组。如果第一个参数是该序列中任何类型的实例,它将返回 true :

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

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

换句话说,isinstance()已经提供了这个功能,开箱即用。

From the isinstance()documentation:

isinstance()文档

If classinfois 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))也是一个有效的选择。

回答by Martin Thoma

You were pretty close with the title of your question already. You could use anyand 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 isinstancereveals:

但是查看isinstance揭示的文档:

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.

如果 object 参数是 classinfo 参数或其(直接、间接或虚拟)子类的实例,则返回 true。如果 object 不是给定类型的对象,则该函数始终返回 false。如果 classinfo 不是类(类型对象),则它可能是类型 objects 的元组,或者可能递归包含其他此类元组(不接受其他序列类型)。如果 classinfo 不是类型或类型元组和此类元组,则会引发 TypeError 异常。

This means the better way to do it is

这意味着更好的方法是

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

回答by Chad S.

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

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

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

回答by Nathaniel Ford

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 documentationthere are a lot of ways you can pass values of types in to isinstance.

根据文档,有很多方法可以将类型的值传递给isinstance.

You might also look into voluptuousif you're trying to do a more complicated validation of which this is just a part.

如果您尝试进行更复杂的验证,这只是其中的一部分,您也可能会考虑voluptuous