class Node:
def __init__(self, value):
self._value_plus = value + 10
self._value_current = value
self._children = []
def __repr__(self):
return 'ooo{!r}'.format(self._value_plus)
def add_child(self, node):
display('transfored_node_dict:{}'.format(node.__dict__))
display('transfored_node:{}'.format(node))
self._children.append(node)
def __iter__(self):
return iter(self._children)
# Example
if __name__ == '__main__':
root = Node(0)
child1 = Node(1)
child2 = Node(2)
root.add_child(child1)
print(child1.__dict__)
print(root.__dict__)
程序输出:
“transfored_node_dict:{’_value_plus’: 11, ‘_value_current’: 1, ‘_children’: []}”
‘transfored_node:ooo11’
{’_value_plus’: 11, ‘_value_current’: 1, ‘_children’: []}
{’_value_plus’: 10, ‘_value_current’: 0, 'children’: [ooo11]}
通过修改_repr() 函数的return的值,可以更改add_child函数中node的输出格式。