I am new to Python and working with a set of objects (Node) and just trying to iterate over the set of objects and printout the variable ‘H’. Unfortunately I keep getting the error: (“AttributeError: ‘NoneType’ object has no attribute ‘H'”). Any insight as to why this is happening would be greatly appreciated.
Here is my class Node that is stored in the set.
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
Here is the set with the first Node being entered. Later on more nodes are entered but for now just adding one is still creating a headache
open_list = set()
start_row, start_col = start_loc
open_list.add(Node(start_row, start_col, 0))
And here is the line of code throwing the error: (“AttributeError: ‘NoneType’ object has no attribute ‘H'”)
for open_node in open_list:
sys.stdout.write("H: " + str(open_node.H))
Once I can get this worked out the real goal is to sort on the Heuristic.
current = sorted(open_list, key=lambda open: open.H)[0]
The error “AttributeError: ‘NoneType’ object has no attribute ‘H'” means that one of the Nodes in
open_listis being assigned the value ‘None’ instead of being initialized. Does anything happen toopen_listbetween the lines you show and the line with an error?