Python: check if an object is a sequence

后端 未结 8 2030
长情又很酷
长情又很酷 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 10:05

    The Python 2.6.5 documentation describes the following sequence types: string, Unicode string, list, tuple, buffer, and xrange.

    def isSequence(obj):
        return type(obj) in [str, unicode, list, tuple, buffer, xrange]
    

提交回复
热议问题