How to compare type of an object in Python?

后端 未结 14 2480
闹比i
闹比i 2020-11-28 01:26

Basically I want to do this:

obj = \'str\'
type ( obj ) == string

I tried:

type ( obj ) == type ( string )
<
14条回答
  •  被撕碎了的回忆
    2020-11-28 02:07

    Type doesn't work on certain classes. If you're not sure of the object's type use the __class__ method, as so:

    >>>obj = 'a string'
    >>>obj.__class__ == str
    True
    

    Also see this article - http://www.siafoo.net/article/56

提交回复
热议问题