[英]Random AttibuteError on python3
我面臨一個奇怪的問題。
我使用2to3將一塊python2更改為python3。
在此之后,我的代碼將隨機運行/失敗...錯誤時出現的錯誤與屬性錯誤相關...但是在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'
我有一本包含“寬度”鍵的字典。 在一個子元素中,我想相對於頂部的寬度評估所述子元素的大小,以查看其是否合適。
這就是為什么我嘗試訪問self.parent_reg.size()來訪問寬度,並隨機發出頂級字典中不存在屬性width的問題。
謝謝你的幫助!
好的,謝謝您的幫助,@ Bryan Oakley,您的幫助幫助了我。
最后,問題是我從加載的json獲取密鑰,並且加載是在字典上完成的,而不是在已排序的字典上完成的。
我的一個鍵(最后一個位於json中)然后調用需要設置其他鍵(報告的錯誤中的寬度)的函數。
不對字典進行排序會使寬度鍵在某些情況下在設置最后一個鍵之前設置,而在其他情況下則不設置。
我仍然感到困惑,為什么它適用於python2。
感謝你的幫助 !
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.