python3上的随机AttibuteError

时间:2017-04-27 11:58:17

标签: python python-3.x attributeerror

我面临一个奇怪的问题。

我使用2to3改变了python2的一部分。

在此之后,我的代码随机运行/失败... 我失败时得到的错误与属性错误有关... 但是在python2和随机python3试验中它可以工作,并找到属性......

我在两台机器上测试了这种行为,同时运行了python3.4。

你是否有任何问题的线索(我对此感到困惑"随机"失败)?

这是我得到的痕迹:

Traceback (most recent call last):  
File "tools/ciregs.py", line 1596, in <module>  
module = Module(json_data)  
File "tools/ciregs.py", line 1147, in \__init__  
self.registers = [Register(json_reg, parent_module=self) for json_reg in  json_module[key]]  
File "tools/ciregs.py", line 1147, in <listcomp>
self.registers = [Register(json_reg, parent_module=self) for json_reg in json_module[key]]  
File "tools/ciregs.py", line 1260, in \__init__
self.fields = [Field(json_field, self) for json_field in json_reg[key]]  
File "tools/ciregs.py", line 1260, in <listcomp>
self.fields = [Field(json_field, self) for json_field in json_reg[key]]  
File "tools/ciregs.py", line 1443, in \__init__
self.check()         
File "tools/ciregs.py", line 1453, in check
if(self.bitWidth <= 0 or self.bitWidth > self.parent_reg.size()):  
File "tools/ciregs.py", line 1326, in size return self.parent_module_.width
AttributeError: 'Module' object has no attribute 'width'

我有一个字典,其中包含&#34;宽度&#34;键。在一个子元素中,我想根据顶部的宽度来评估所述子元素的大小,看它是否适合。

这就是为什么我尝试访问self.parent_reg.size()访问宽度并随机发出该属性宽度并不存在于顶级字典中的原因。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

好的,谢谢你的帮助,@ Bryan Oakley,你的帮助帮助了我。

最后问题是我从json加载我的密钥,并且加载是在字典而不是排序字典上完成的。

我的一个密钥,最后一个在json中,然后调用期望其他密钥(在报告的错误中的宽度)的函数被设置。

如果字典未排序,则在设置最后一个键之前的某些情况下会设置宽度键,而在某些其他情况下不会设置。

我仍然感到困惑,为什么它适用于python2 ....

感谢大家的帮助!