Check if object is a number or boolean

后端 未结 7 1044
攒了一身酷
攒了一身酷 2021-02-06 20:42

Design a logical expression equivalent to the following statement:

x is a list of three or five elements, the second element of which is

相关标签:
7条回答
  • 2021-02-06 21:35

    You should compare the type of x to the bool class:

    type(x) == bool
    

    or:

    type(x) == type(True)
    

    Here is more on the type method

    From Data model docs:

    Booleans (bool)

    These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

    0 讨论(0)
提交回复
热议问题