简体   繁体   English

Python 2.7'NoneType'对象没有属性

[英]Python 2.7 'NoneType' object has no attribute

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'. 我是Python的新手,正在使用一组对象(节点),只是尝试遍历一组对象并打印出变量“ H”。 Unfortunately I keep getting the error: ("AttributeError: 'NoneType' object has no attribute 'H'"). 不幸的是,我一直收到错误消息:(““ AttributeError:'NoneType'对象没有属性'H'”)。 Any insight as to why this is happening would be greatly appreciated. 对于为什么发生这种情况的任何见解将不胜感激。

Here is my class Node that is stored in the set. 这是存储在集合中的我的班级Node。

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'") 这是引发错误的代码行:(“” AttributeError:'NoneType'对象没有属性'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_list is being assigned the value 'None' instead of being initialized. 错误“ AttributeError:'NoneType'对象没有属性'H'”意味着在open_list中的节点之一被分配了值“ None”,而不是被初始化。 Does anything happen to open_list between the lines you show and the line with an error? 您显示的行和有错误的行之间的open_list发生了什么事情?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM