我想向 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)
任何有关执行此操作或实现过程中需要考虑的其他事项的建议都会很好。谢谢!
最佳答案
您正在 Python 2 上使用 pyTree
;但该项目仅适用于 Python 3。
来自PyPI page :
Python 3 中的A list-derived TREE data structure in Python 3
super()
在方法中使用时不带参数,并且 that's what the pyTree
project does 。这使得代码库与 Python 2 不兼容。
否则你正确地扩展了类;您的代码与 Python 3 一起使用时可以工作。
关于python - 拓展新式类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32964772/