如何打印整个父节点子结构

How to print whole parent - node - child structure

我想用打印功能显示连接到特定节点的所有内容,但到目前为止我无法避免使用全局来这样做。如何让我的 __repr__function 更干净、更本地化?到目前为止我得到的完整代码如下:

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

您可以在没有 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)

我在 https://repl.it/repls/ZanyKlutzyBase

做了一个包含更多嵌套节点的示例

您可以给 __repr__ 更多默认参数:

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)

一种简单的方法是,特殊方法 __repr__ 将实际处理委托给递归方法。为了避免界面混乱,该方法的名称可以以下划线开头 (_)。代码可以是:

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

这符合预期:

Node 1 ↴
    Node 2
    Node 3 ↴
        Node 4
    Node 5