简体   繁体   中英

Extending a new-style class

I'd like to add some extra attributes to the pyTree node class for implementing a decision tree algorithm.

It has only one data attribute for user defined contents, which is also the attribute searched when using the class method getNode(content) . I am thinking to store a unique ID there, but also store other attributes for tree calculations.

I've tried doing various things, but it appears from this post , that the following should be the way to do it:

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')

Which results in the following attribute error:

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

Any suggestions on doing this or other things to consider with the implementation would be great. Thanks!

You are using pyTree on Python 2; but the project only works on Python 3.

From the PyPI page :

A list-derived TREE data structure in Python 3

super() in Python 3 takes no arguments when used in a method, and that's what the pyTree project does . This makes the codebase incompatible with Python 2.

You were otherwise correctly extending the class; your code works when used with Python 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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