天天看点

Python报错:AttributeError: type object 'str' has no attribute '_name_'(机器学习实战treePlotter代码)解决方案

报错信息

学习《机器学习实战》这本书时,按照书上的代码运行,产生了错误,但是在代码中没有错误提示,产生错误的代码如下:

if type(secondDict[key])._name_ == 'dict':      

报错如下:

Python报错:AttributeError: type object 'str' has no attribute '_name_'(机器学习实战treePlotter代码)解决方案

错误原因

首先我们先看一下报错:

AttributeError: type object 'str' has no attribute '_name_'

翻译过来是:

属性错误:类型对象“ str ”没有属性“_name_”,

错误产生是因为版本不同,作者使用的是2.x版本,而我使用的是3.6版本。

解决方案

if type(secondDict[key]) == dict:      

继续阅读