检查一个对象是否是另一个对象的参数

Checking whether an object is a parameter in another object

我目前正在 Python 构建一个 HTML 渲染器并让 类 处理标签:

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 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)
        return ""
        

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)
        return ""

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):
        super().__init__(inner_html)
from tag import Html, P, Div, Head, Style, Body
 
html = Html([
        Div([
            Head([])
        ])    
    ])

print(html.render())
<!DOCTYPE html>
<html>
        <div>
        <head>
        </head>

        </div>

</html>

我的单个标签在一个包含标签内打印时效果很好,但是当在另一个包含标签内打印一个包含标签时,缩进处于同一水平,而我希望它缩进。有没有办法检查一个包含标签是否是另一个包含标签的子标签,这样我就可以有条件地打印额外的缩进?

我一直这样处理递归缩进的方式是使用 render 的关键字参数(或等效函数的任何名称)。

此外,您可以通过继承 property 函数来节省大量代码,这些函数的作用类似于属性,而不是在 __init__ 函数中设置静态值。

class Tag:

    @property
    def open_tag(self): return f'<{self.__class__.__name__.lower()}>'

    @property
    def close_tag(self): return f'</{self.__class__.__name__.lower()}>'

    def __str__(self):
        return self.render()

class SingleTag(Tag):

    def __init__(self, inner_html):
        self.inner_html = inner_html

    def render(self, indent=''):
        return f'{indent}{self.open_tag}{self.inner_html}{self.close_tag}\n'

class ContainingTag(Tag):

    def __init__(self, children):
        self.children = children

    def render(self, indent=''):
        inner = ''.join(child.render(indent + '\t') for child in self.children)
        return f'{indent}{self.open_tag}\n{inner}{indent}{self.close_tag}\n'

class Html(ContainingTag):
    '''HTML tag'''

    @property
    def open_tag(self): return '<!DOCTYPE html>\n<html>'
    
class Head(ContainingTag): '''HEAD tag'''

class Style(ContainingTag): '''STYLE tag'''

class Body(ContainingTag): '''BODY tag'''

class Div(ContainingTag): '''DIV tag'''

class P(SingleTag): '''P tag'''

然后:

html = Html([Div([Head([])])])
print(html)
<!DOCTYPE html>
<html>
    <div>
        <head>
        </head>
    </div>
</html>

注意:我在这里使用 \t 作为 per-level 缩进,但您可以使用任何适合您需要的缩进。