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

发布于 2025-01-24 00:44:52 字数 2178 浏览 0 评论 0原文

因此,我目前正在Python中构建HTML渲染程序,该程序使用两个基类:

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

    tag

  2. 包含标签的标签,用于嵌套标签的标签例如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

在单元格对象上使用渲染方法时,它会按预期呈现,但是使用渲染时在包含tag上,它在每个关闭标签之后都打印“无”:

<Opening ContainingTag>
<Closing ContainingTag>
None

有人可以解释为什么这会继续打印以及如何解决此问题?谢谢。

So I'm currently building a Html rendering program in Python which uses two base classes:

  1. SingleTag - Used for tags that don't have any children such as a

    tag

  2. ContainingTag - For tags that have nested tags such as the Html tag, Div tag etc...

Here is the file which contains those classes, along with the subclasses that inherit from them:

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

When using the render method on a SingleTag object, it renders as expected, but when using the render method on a ContainingTag, it prints 'None' after every closing tag like this:

<Opening ContainingTag>
<Closing ContainingTag>
None

Can someone explain why this keeps printing and how to fix this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

dawn曙光 2025-01-31 00:44:52

更简单的方法:

  • 覆盖内置__ str __()方法,而不是创建render
  • 将标签名称作为参数传递给类别概述,因此您无需创建许多子类(您可能仍需要创建html子类)
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>

A simpler approach:

  • Override the built-in __str__() method instead of creating render
  • Pass the tag name as a parameter to the class contructor, so you don't need to create a lot of subclasses (you may still want to create a HTML subclass)
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>
岁月静好 2025-01-31 00:44:52

错误似乎是渲染函数实际上没有返回任何内容,因此返回默认的IE 将返回。

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

The error seems to be that the render function does not actually return anything so the default i.e None is 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文