Python: check if an object is a sequence

后端 未结 8 2043
长情又很酷
长情又很酷 2020-12-05 09:17

In python is there an easy way to tell if something is not a sequence? I tried to just do: if x is not sequence but python did not like that

8条回答
  •  不知归路
    2020-12-05 09:42

    why ask why

    try getting a length and if exception return false

    def haslength(seq):
        try:
            len(seq)
        except:
            return False
        return True
    

提交回复
热议问题