1

我在我的 Python 应用程序中使用 lxml.objectify 并为选定的 XML 元素定义了自定义元素类。当我向自定义元素类添加属性时,会调用 getter,但不会调用 setter。没有调用 setter,而是将具有我定义的属性名称的新子元素添加到元素中。

我假设我的 getter 定义正确地覆盖了__getattr__ObjectifiedElement 的方法,但由于某种原因__setattr__,ObjectifiedElement 的方法优先于我的 setter。

让它执行我的设置器需要什么?

class CT_TextParagraph(objectify.ObjectifiedElement):
    """<a:p> custom element class"""
    def _get_algn(self):
        """
        Value of algn attribute on <a:pPr> child element
        """
        if not hasattr(self, 'pPr'):
            return None
        return self.pPr.get('algn')

    def _set_algn(self, value):
        """
        Set value of algn attribute on <a:pPr> child element
        """
        raise AssertionError('in _set_algn()')
        # if not hasattr(self, 'pPr'):
        #     pPr = _Element('a:pPr')
        #     self.insert(0, pPr)
        self.pPr.set('algn', value)

    #: Paragraph horizontal alignment value, like ``TAT.CENTER``
    algn = property(_get_algn, _set_algn)
4

0 回答 0