Python 如何打印整个父节点子结构
我想用打印功能显示连接到特定节点的所有内容,但到目前为止,我无法避免使用全局功能。如何使我的_repr__________________________函数更干净、更本地化?我得到的全部代码如下:Python 如何打印整个父节点子结构,python,python-3.x,printf,nodes,parent,Python,Python 3.x,Printf,Nodes,Parent,我想用打印功能显示连接到特定节点的所有内容,但到目前为止,我无法避免使用全局功能。如何使我的_repr__________________________函数更干净、更本地化?我得到的全部代码如下: current_parent = None indent_increase = 1 class Node(object): def __init__(self, data): self.data = data self.parent = None
current_parent = None
indent_increase = 1
class Node(object):
def __init__(self, data):
self.data = data
self.parent = None
self.children = []
def add_child(self, obj):
self.children.append(obj)
obj.parent = self.data
def __repr__(self):
global current_parent
global indent_increase
if self.children:
print_data = ""
print_data += "Node " + str(self.data) + " ↴" + "\n"
indent = " "
for child in self.children:
if current_parent != child.parent:
current_parent = child.parent
indent_increase += 1
print_data += ((indent * indent_increase) + str(child) + "\n")
else:
print_data += ((indent * indent_increase) + str(child) + "\n")
indent_increase = 1
current_parent = 0
return print_data
else:
return "Node " + str(self.data)
a = Node(1)
b = Node(2)
c = Node(3)
d = Node(4)
e = Node(5)
c.add_child(d)
a.add_child(b)
a.add_child(c)
a.add_child(e)
print(a)
期望输出:
Node 1 ↴
Node 2
Node 3 ↴
Node 4
Node 5
Node 1 ↴
Node 2
Node 3 ↴
Node 4
Node 5
您可以不使用
globals
——只需将嵌套节点的结果拆分为行,并在返回结果之前缩进每个行:
class Node(object):
def __init__(self, data):
self.data = data
self.parent = None
self.children = []
def add_child(self, obj):
self.children.append(obj)
obj.parent = self
def __repr__(self):
if self.children:
print_data = "Node " + repr(self.data) + " ↴" + "\n"
indent = " "
for child in self.children:
for line in repr(child).splitlines():
print_data += indent + line + "\n"
return print_data
else:
return "Node " + repr(self.data)
我在中使用了更多嵌套节点的示例,您可以使用默认值提供更多参数:
class Node(object):
def __init__(self, data):
self.data = data
self.parent = None
self.children = []
def add_child(self, obj):
self.children.append(obj)
obj.parent = self.data
def __repr__(self, current_parent=None, indent_increase=1):
if self.children:
print_data = ""
print_data += "Node " + str(self.data) + " ↴" + "\n"
indent = " "
for child in self.children:
if current_parent != child.parent:
print_data += ((indent * indent_increase) + child.__repr__(child.parent, indent_increase + 1) + "\n")
else:
print_data += ((indent * indent_increase) + str(child) + "\n")
return print_data
else:
return "Node " + str(self.data)
a = Node(1)
b = Node(2)
c = Node(3)
d = Node(4)
e = Node(5)
c.add_child(d)
a.add_child(b)
a.add_child(c)
a.add_child(e)
print(a)
一种简单的方法是,特殊方法
\uuuu repr\uuu
将实际处理委托给递归方法。为了避免界面混乱,该方法的名称可以以下划线(\uu
)开头。代码可以是:
class Node(object):
def __init__(self, data):
self.data = data
self.parent = None
self.children = []
def add_child(self, obj):
self.children.append(obj)
obj.parent = self.data
def __repr__(self):
return self._do_repr(0)
def _do_repr(self, indent_increase):
indent = " "
print_data = (indent * indent_increase) + "Node " + str(self.data)
if self.children:
print_data += " ↴" + "\n"
for child in self.children:
print_data += child._do_repr(indent_increase + 1)
else:
print_data += '\n'
return print_data
正如预期的那样: