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

后端 未结 13 1041
南旧
南旧 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:08

    isinstance(o, str) will return True if o is an str or is of a type that inherits from str.

    type(o) is str will return True if and only if o is a str. It will return False if o is of a type that inherits from str.

提交回复
热议问题