为什么这个' Node'对象没有属性' daughters'?

时间:2015-07-25 16:57:17

标签: python

这些是我的GraphNode类:

import random

class Graph:
    def __init__(self):
        self.node_list = {}
        self.number = 0

    def node(self, node_name):
        if node_name in self.node_list: #returns list of keys
            return self.node_list[node_name]
        else:
            self.node_list[node_name] = Node()

    def name_node(self):
        if names == False:
            names == []
        if len(names) < 2: 
            for count in range(2 - len(names)):
                names.append('node_' + str(number))
                number += 1
        if names[0] in node_list:
            del names[0]
            return name_node()
        return names.pop(0)
        ...
    class Node:
        def __init__(self):
            daughters = []
            ...

def PrefixTrieConstruction(patterns):
    trie = Graph()
    trie.node('root')

    for pattern in patterns:
        currentNode = trie.node('root')
        for symbol in pattern:
            for daughter in currentNode.daughters:
                if daughter.label == symbol:
                    currentNode = daughter
                    break
            else:
                node_name = Trie.name_node()
                Trie.node(node_name)
                Trie.edge(currentNode, node_name, symbol)
                currentNode = node_name
    return Trie

当我运行它时,它返回一个参考行

的错误
            for daughter in currentNode.daughters:

Node object has no attribute 'daughters'

我把它放在init中,所以我相信每个节点都必须有daughters = []。 我还尝试在Graph.node方法else子句下面添加一行,明确说明返回新创建的节点。

为什么这个属性不存在?

1 个答案:

答案 0 :(得分:2)

Node.__init__中,您设置了一个局部变量daughters,而不是一个属性self.daughters