'list' object has no attribute . Cant set a new attribute?
当我构思标题问题时,我看到了这个https://stackoverflow.com/questions/23642105/attributeerror-list-object-has-no-attribute-trackprogress和attributeerror:list"object在python中没有属性"display"。
但与这些问题不同,这类似于不能设置对象类的属性,并且只有当列表的行为与对象不同时才不重复(保留,只读属性)
我可以设置self.root并检查self.rightchild&self.leftchild那么为什么它会抛出这个错误呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class BinaryTree(list): ...: def __init__(self,root_val,**kwarg): ...: super(type(self),self).__init__() ...: self.root = root_val ...: self.rightChild = self.leftChild = None ...: self = binaryTree(self.root) ...: if 'left_child' in kwarg: ...: self.leftChild = kwarg['left_child'] ...: if 'right_child' in kwarg: ...: self.rightChild = kwarg['right_child'] ...: b = BinaryTree(4,left_child = 5,right_child = 6) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-67-3ea564fafb06> in <module>() ----> 1 b = BinaryTree(4,left_child = 5,right_child = 6) <ipython-input-66-208c402b07ce> in __init__(self, root_val, **kwarg) 6 self = binaryTree(self.root) 7 if 'left_child' in kwarg: ----> 8 self.leftChild = kwarg['left_child'] 9 if 'right_child' in kwarg: 10 self.rightChild = kwarg['right_child'] AttributeError: 'list' object has no attribute 'leftChild' |
binarytree是一些foo()函数,它工作得很好,一点也不重要。
显然,
binaryTree is [...] not at all the concern
号
我很确定你是错的。
将
(同样,覆盖