【问题标题】:Extending a new-style class扩展一个新型类
【发布时间】:2015-10-06 08:06:58
【问题描述】:

我想为 pyTree node 类添加一些额外的属性来实现决策树算法。

用户自定义内容只有一个data属性,这也是使用类方法getNode(content)时搜索到的属性。我正在考虑在其中存储一个唯一 ID,同时也存储其他属性以进行树计算。

我尝试过做各种事情,但从this post 看来,应该采用以下方法:

from pyTree.Tree import Tree 

class decTree(Tree):
    def __init__(self, val1, val2, data=None, children=None,):
        super(decTree, self).__init__(data=None, children=None)
        self.val1 = val1
        self.val2 = val2

if __name__ == '__main__':
    tree = decTree(val1=1.5, val2='string', data='567')

这会导致以下属性错误:

TypeError: super() takes at least 1 argument (0 given)

任何有关执行此操作或其他要考虑实施的事情的建议都会很棒。谢谢!

【问题讨论】:

  • 异常的完整回溯是什么?显然,TypeError 并非源于您在这里使用super(),您有两个参数。
  • pyTree 需要 Python 3 或更高版本。

标签: python class


【解决方案1】:

您在 Python 2 上使用 pyTree;但该项目仅适用于 Python 3。

来自PyPI page

Python 3

中的列表派生树数据结构

super() 在 Python 3 中用于方法时不带参数,that's what the pyTree project does。这使得代码库与 Python 2 不兼容。

您在其他方面正确地扩展了课程;您的代码在与 Python 3 一起使用时有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多