简体   繁体   English

没有外部验证器或type()的Python 3 isinstance bool?

[英]Python 3 isinstance bool without external validator or type()?

so I'm trying to pass through a list and identify the different int, float, and bool. 所以我试图通过一个列表,并确定不同的int,float和bool。 Now I'm aware after research from stack here that bool is a subclass of int as per the initial Python versions and that I need some special handling to identify bool from int like so: 现在,从这里的堆栈中进行研究后,我知道按照最初的Python版本,bool是int的子类,并且我需要一些特殊的处理才能从int识别出bool,如下所示:

listone = ['232.54',False,'656',27.3,'5',' ',True,'-434','76.67','8.45','s',23]

for i in listone:
if isinstance(i,int):
    print(i,'INT')
elif isinstance(i,float):
    print(i,'FLOAT')
elif isinstance(i,str):
    print(i,'STR')

elif isinstance(i,bool):
    #SPECIAL HANDLING 
    print(i,'BOOL')

However even after doing so I still get the printout: 但是即使这样做,我仍然可以得到打印输出:

232.54 STR
False INT
656 STR
27.3 FLOAT
5 STR
STR
True INT
-434 STR
76.67 STR
8.45 STR
s STR
23 INT

Now, is there any way that I can get this printed out with some special handling, without using external validator like this or by using "type() == "? 现在,有什么方法可以通过一些特殊处理将其打印出来,而无需使用像这样的外部验证器或通过使用“ type()==”? Thanks 谢谢

You're on the right track to observe that bool is a subclass of int . 您在正确的位置上观察到boolint的子类。 The issue is the behavior of isinstance . 问题是isinstance的行为。 From the documentation (emphasis added): 文档 (添加重点):

isinstance ( object, classinfo ) isinstanceobject,classinfo

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 a tuple of type objects (or recursively, other such tuples), return true if object is an instance of any of the types. 如果classinfo是类型对象的元组(或递归,其他类似的元组),则如果object是任何类型的实例,则返回true。 If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised. 如果classinfo不是类型或此类的元组,则引发TypeError异常。

Since bool is a subclass of int , any bools passed into your if/elif clauses will not make it past the first if statement, isinstance(<bool>, int) will return True . 由于boolint的子类,因此传递给您的if / elif子句的任何bool都不会使其超出第一个if语句,因此isinstance(<bool>, int)会返回True You need to put the check for bool before the check for int . 您需要将bool支票放在int支票之前。

Just change the order of your checks to check for bool first. 只需更改检查顺序即可先检查bool That way it won't get caught by your check for int . 这样,它就不会被您的int支票所抓住。

listone = ['232.54',False,'656',27.3,'5',' ',True,'-434','76.67','8.45','s',23]

for i in listone:

    if isinstance(i, bool):
        #SPECIAL HANDLING 
        print(i, 'BOOL')
    elif isinstance(i, int):
        print(i, 'INT')
    elif isinstance(i, float):
        print(i, 'FLOAT')
    elif isinstance(i, str):
        print(i, 'STR')

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

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