Check if object is a number or boolean

后端 未结 7 1001
攒了一身酷
攒了一身酷 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:17

    I like to keep it simple to read.. This will accept bool, string, number and read it to a bool

    def var2bool(v):
    if type(v) == type(True):
        res = v
    elif type(v) == type(0):
        if v == 0:
            res = False
        else:
            res = True
    elif v.lower() in ("yes", "true", "t", "1"):
        res = True
    else:
        res = False
    return res
    

提交回复
热议问题