我是Python新手,使用一組對象(Node)並試圖迭代對象集合並打印出變量'H'。不幸的是我不斷收到錯誤:(「AttributeError:'NoneType'對象沒有屬性'H'」)。任何有關爲什麼會發生這種情況的洞察力將不勝感激。Python 2.7'NoneType'對象沒有任何屬性
這是我的類節點,存儲在集合中。
class Node:
def __init__(self, row, col, heuristic):
self.row = row
self.col = col
self.H = heuristic
self.parent = None
@classmethod
def with_parent(self, row, col, heuristic, parent):
self.row = row
self.col = col
self.H = heuristic
self.parent = parent
這裏是輸入第一個節點的集合。後來,更多的節點進入,但現在只需添加一個正在製造一個頭痛
open_list = set()
start_row, start_col = start_loc
open_list.add(Node(start_row, start_col, 0))
這裏是代碼引發錯誤行:(「AttributeError的:‘NoneType’對象有沒有屬性‘H’」 )
for open_node in open_list:
sys.stdout.write("H: " + str(open_node.H))
一旦我能得到這個解決方案,真正的目標是排序在啓發式。
current = sorted(open_list, key=lambda open: open.H)[0]
您在表達式'open_node.H'處得到此異常,因爲在您的程序的某個位置'open_node'中的值爲'None'。 – wberry
您顯示的代碼沒有任何錯誤。嘗試創建一個小的test.py,它將重現問題 –