【发布时间】:2018-12-02 08:40:55
【问题描述】:
我正在尝试将 ete3.Tree 的所有功能继承到名为 TreeAugmented 的新类中,但并非所有方法和属性都可用?
在__init__ 和super 中有什么我应该做的吗?似乎super 必须指定单独的属性,如The inheritance of attributes using __init__。
我可以在名为 tree 的类中有另一个对象,我在其中存储来自 ete3.Tree 的所有内容,但我希望能够将这些对象与 ete3 包中的函数一起使用。
有没有办法只继承父类的所有内容?
import ete3
newick = "(((petal_width:0.098798,petal_length:0.098798):0.334371,"
"sepal_length:0.433169):1.171322,sepal_width:1.604490);"
print(ete3.Tree(newick).children)
# [Tree node '' (0x1296bf40), Tree node 'sepal_width' (0x1296bf0f)]
class TreeAugmented(ete3.Tree):
def __init__(self, name=None, new_attribute=None):
self.name = name # This is an attribute in ete3 namespace
self.new_attribute = new_attribute
x = TreeAugmented(newick)
x.children
追溯
AttributeError Traceback (most recent call last)
<ipython-input-76-de3016b5fd1b> in <module>()
9
10 x = TreeAugmented(newick)
---> 11 x.children
~/anaconda/envs/python3/lib/python3.6/site-packages/ete3/coretype/tree.py in _get_children(self)
145
146 def _get_children(self):
--> 147 return self._children
148 def _set_children(self, value):
149 if type(value) == list and \
AttributeError: 'TreeAugmented' object has no attribute '_children'
【问题讨论】:
-
你通常调用基类的
__init__()。具体取决于 Python 版本。 -
我正在使用 Python 3.6,并计划在我使用的所有包也迁移后升级到 3.7。所以在
__init__中我会调用ete3.Tree类? -
如果您查看您发布的链接的接受答案,它会显示代码在没有显式调用所有属性的情况下工作。父类需要的任何参数仍然是必需的,所以不要忘记也传递它们。
标签: python class object inheritance super