What's the canonical way to check for type in Python?

后端 未结 13 1061
南旧
南旧 2020-11-21 07:34

What is the best way to check whether a given object is of a given type? How about checking whether the object inherits from a given type?

Let\'s say I have an objec

13条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 08:04

    A simple way to check type is to compare it with something whose type you know.

    >>> a  = 1
    >>> type(a) == type(1)
    True
    >>> b = 'abc'
    >>> type(b) == type('')
    True
    

提交回复
热议问题