[Tutor] variable inheritance
spir
denis.spir at gmail.com
Mon Mar 8 13:57:39 CET 2010
Hello,
Say I have a Tree type that may be based on 2 kinds of Node-s. Both conceptually and practically, a tree itself is a node, namely the top/root one. But a tree also has some additional tree-level behaviour, which is independant of the kind of node. Say, the node type performs all the underlying tree mechanics. Thus the Tree type looks like:
class Tree(_Node):
def __init__(self, args):
_Node.__init__(self)
...
... tree-level methods ...
The issue is the actual kind of node is not only initialised through "_Node.__init__(self)": it must first feature as super class in "class Tree(Node)". (Otherwise I get an error about wrong type of self.)
I tried to play with __new__ in Tree and Node types, but this error seems unavoidable. Maybe I did not do it right. I can indeed have __new__ return an instance of either Node type, but then for any reason it "forgets" it is also a tree (so that calling any tree method fails).
I there a way to do this right?
Workaround ideas:
* Make the root node an attribute of tree: unsatisfying.
* Create a common tree type and 2 specialised ones:
class TreeN(Tree,NodeN)
def __init__(self, args):
_NodeN.__init__(self)
Tree.__init__(self, args)
# ... e basta ...
Denis
--
________________________________
la vita e estrany
spir.wikidot.com
More information about the Tutor
mailing list