@ObjectifiedElement上的属性setter没有

2024-10-16 17:19:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用lxml.objectify在我的应用程序中为Python和自定义元素定义了一些元素。当我向自定义元素类添加属性时,会调用getter而不是setter。将调用元素的子元素的名称添加到元素的setter中,而不是使用新的元素名称。在

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

怎样才能让它执行我的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)

Tags: 方法self名称元素get定义valueelement