How to compare type of an object in Python?

后端 未结 14 2448
闹比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 01:55

    I use type(x) == type(y)

    For instance, if I want to check something is an array:

    type( x ) == type( [] )
    

    string check:

    type( x ) == type( '' ) or type( x ) == type( u'' )
    

    If you want to check against None, use is

    x is None
    

提交回复
热议问题