为什么我的对象渲染函数显示 None?

Why is my object render function displaying None?

所以我目前正在 Python 中构建一个 Html 渲染程序,它使用两个基础 类:

  1. SingleTag - 用于没有任何子标签的标签,例如

    标签

  2. ContainingTag - 对于具有嵌套标签的标签,例如 Html 标签、Div 标签等...

这是包含那些 类 的文件,以及从它们继承的子 类:

class ContainingTag:
    def __init__(self, children):
        self.open_tag = "<" + self.__class__.__name__.lower() + ">"
        self.close_tag = "</"+self.__class__.__name__.lower() + ">"
        self.children = children

    def render(self):
        print("\t" + self.open_tag)
        for child in self.children:
            print("\t \t" + str(child.render()))
        print("\t" + self.close_tag)
class SingleTag:
    """A class to represent an html tag"""

    # Class initialiser
    def __init__(self, inner_html):
        self.open_tag = "<" + self.__class__.__name__.lower() + ">"
        self.close_tag = "</"+self.__class__.__name__.lower() + ">"
        self.inner_html = inner_html

    # Prints html
    def render(self):
        return self.open_tag + self.inner_html + self.close_tag
class Html(ContainingTag):
    def __init__(self, children):
        super().__init__(children) 
        self.open_tag = "<!DOCTYPE html>\n"+ "<" + self.__class__.__name__.lower() + ">"
    
    def render(self):
        print(self.open_tag)

        for child in self.children:
            print("\t \t" + str(child.render()))
        print(self.close_tag)


class Head(ContainingTag):
    def __init__(self, children):
        super().__init__(children)

class Style(ContainingTag):
    def __init__(self, children):
        super().__init__(children)

class Body(ContainingTag):
    def __init__(self, children):
        super().__init__(children)  

class Div(ContainingTag):
    def __init__(self, children):
        super().__init__(children)

class P(SingleTag):
    def __init__(self, inner_html=None):
        super().__init__(inner_html=None)
        self.inner_html = inner_html

在 SingleTag 对象上使用 render 方法时,它会按预期呈现,但在 ContainingTag 上使用 render 方法时,它会在每个结束标记后打印 'None',如下所示:

<Opening ContainingTag>
<Closing ContainingTag>
None

谁能解释为什么会一直打印以及如何解决这个问题?谢谢。

错误似乎是 render 函数实际上没有 return 任何东西,所以默认值即 None 是 returned。

def render(self):
        print(self.open_tag)

        for child in self.children:
            print("\t \t" + str(child.render()))
        print(self.close_tag)
        # Add a return statement

更简单的方法:

  • 覆盖 built-in __str__() 方法而不是创建 render
  • 将标签名称作为参数传递给 class 构造函数,因此您不需要创建很多子classes(您可能仍想创建一个 HTML子class)
class ContainingTag:
    def __init__(self, name, children):
        self.name = name
        self.children = children

    def __str__(self):
        return f'<{self.name}>\n' + ''.join([str(c) for c in self.children]) + f'</{self.name}>\n'
        
class SimpleTag:
    def __init__(self, name, html):
        self.name = name
        self.html = html

    def __str__(self):
        return f'<{self.name}>{self.html}</{self.name}>\n'

p1=SimpleTag('P', 'Hello')
p2=SimpleTag('P', 'World')
d=ContainingTag('DIV', [p1,p2])
b=ContainingTag('BODY', [d])

print(str(p1))
<P>Hello</P>

print(str(b))
<BODY>
<DIV>
<P>Hello</P>
<P>World</P>
</DIV>
</BODY>