转自:http://www.jb51.net/article/96469.htm
isinstance方法判断一个对象是否属于某类,isinstance(object, classinfo)
例如:isinstance(1, int) 返回True
isinstance(a,dict) 判断对象a是否为字典,如果为真,会打印True,如为假,打印False。
应用示例:
def list_all_dict(dict_a):
if isinstance(dict_a,dict) : #使用isinstance检测数据类型
for x in dict_a:
#temp_key = dict_a.keys()[x]
temp_value = dict_a[x]
#print ("%s %s"%(x, temp_value))
list_all_dict(temp_value) #自我调用实现无限遍历
else:
print (dict_a)
person = {"male":{"name":"Shawn"}, "female":{"name":"Betty","age":23},"children":{"name":{"first_name":"李", "last_name":{"old":"明明","now":"铭"}},"age":4}}
list_all_dict(person)