为什么我的对象渲染函数不显示?
因此,我目前正在Python中构建HTML渲染程序,该程序使用两个基类:
- Singletag-用于没有任何孩子的标签,例如
tag
- 包含标签的标签,用于嵌套标签的标签例如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:
- SingleTag - Used for tags that don't have any children such as a
tag
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更简单的方法:
__ str __()
方法,而不是创建render
html
子类)A simpler approach:
__str__()
method instead of creatingrender
HTML
subclass)错误似乎是
渲染
函数实际上没有返回任何内容,因此返回默认的IE无
将返回。The error seems to be that the
render
function does not actually return anything so the default i.eNone
is returned.