Determine if Python variable is an instance of a built-in type

前端 未结 9 2345
难免孤独
难免孤独 2020-12-05 23:39

I need to determine if a given Python variable is an instance of native type: str, int, float, bool, list, <

9条回答
  •  臣服心动
    2020-12-06 00:17

    You appear to be interested in assuring the simplejson will handle your types. This is done trivially by

    try:
        json.dumps( object )
    except TypeError:
        print "Can't convert", object
    

    Which is more reliable than trying to guess which types your JSON implementation handles.

提交回复
热议问题