Python 组合导致 AttributeError

标签 python oop nlp

我正在构建一个有限状态接受器(FSA...基本上是一棵树)。我有一个 FSA 类和一个 State 类,因为 FSA 应该由许多 State 组成。但是,当最初创建 FSA 时,我想创建它的开始 State 和结束 State,并将它们连接起来。这是相关代码:

class State (object):
    __slots__ = "chars","children"
    def __init__(self,chars,children=[]):
        self.chars = chars
        self.children = children

class FSA (object):
    __slots__ = "vocab"
    def __init__(self,vocab):
        self.vocab = vocab
        self.start = State("0")
        self.finish = State("1")
        self.start.children.append(self.finish)
        self.finish.children.append(self.start)

但是,当我运行 fsa = FSA("vocab.small") 时,会抛出错误:

AttributeError: 'FSA' object has no attribute 'start'

有没有一种方法可以启动 FSA 并为其创建两个 State

最佳答案

你说:

__slots__ = "vocab"

你这是什么意思?如果你想有一个 start 属性,你需要将它包含在你的 __slots__ 列表中,或者根本不使用 __slots__。除非您有内存问题,否则最好省略 __slots__ 属性。

关于Python 组合导致 AttributeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15035279/

相关文章:

python - Cython 的扩展模块的类的方法不可见/给出名称提及时出错

Python父类访问子私有(private)变量

python - 使用SVD绘制词向量来衡量相似度

python-3.x - 对中英文文本进行分词不正确地将英文单词拆分为字母

python - 如何将 pandas.series.groupby() 表从计数转换为百分比频率?

python - pandas 或 numpy 表示三个月的总和。以列表形式返回

javascript - Angular/Typescript——通过示例解释接口(interface)的好处

oop - 调用层次结构中较高层的类的重写 protected 方法

python - 使用 Python-Treetaggerwrapper 进行分块

python - “模块”对象没有属性 'feature_column'