python - 在 python 3 中从父类扩展 __init__ 的正确方法

标签 python python-3.x oop initialization urwid

一般问题:除了添加单个属性外,最简单/“最pythonic”的子类初始化方法是什么?

我的具体问题:我想扩展一个 ( Urwid ) Edit 对象以包含一个附加属性 my_attribute;我已经将原始签名复制到 __init__super().__init__ 中,但是有一些未定义的参数/常量(LEFTSPACE) 在签名中,我不明白它们是如何在父类中设置的。下面是我的(中断)类定义和父初始化方法:

class MyEdit(urwid.Edit):

    def __init__(self, my_attribute, caption="", edit_text="", multiline=False, align=LEFT, wrap=SPACE, allow_tab=False, edit_pos=None, layout=None, mask=None):

        super().__init__(caption="", edit_text="", multiline=False, align=LEFT, wrap=SPACE, allow_tab=False, edit_pos=None, layout=None, mask=None)
        self.my_attribute = []    
        # super().__super.__init__("", align, wrap, layout)

    def my_method(self):
        #some code that modifies my_attribute
        return self.my_attribute




class Edit(Text):
    """
    Text editing widget implements cursor movement, text insertion and
    deletion.  A caption may prefix the editing area.  Uses text class
    for text layout.

    Users of this class to listen for ``"change"`` events
    sent when the value of edit_text changes.  See :func:``connect_signal``.
    """
    # (this variable is picked up by the MetaSignals metaclass)
    signals = ["change"]

    def valid_char(self, ch):
        """
        Filter for text that may be entered into this widget by the user

        :param ch: character to be inserted
        :type ch: bytes or unicode

        This implementation returns True for all printable characters.
        """
        return is_wide_char(ch,0) or (len(ch)==1 and ord(ch) >= 32)

    def selectable(self): return True

    def __init__(self, caption="", edit_text="", multiline=False,
            align=LEFT, wrap=SPACE, allow_tab=False,
            edit_pos=None, layout=None, mask=None):
        """
        :param caption: markup for caption preceeding edit_text, see
                        :class:`Text` for description of text markup.
        :type caption: text markup
        :param edit_text: initial text for editing, type (bytes or unicode)
                          must match the text in the caption
        :type edit_text: bytes or unicode
        :param multiline: True: 'enter' inserts newline  False: return it
        :type multiline: bool
        :param align: typically 'left', 'center' or 'right'
        :type align: text alignment mode
        :param wrap: typically 'space', 'any' or 'clip'
        :type wrap: text wrapping mode
        :param allow_tab: True: 'tab' inserts 1-8 spaces  False: return it
        :type allow_tab: bool
        :param edit_pos: initial position for cursor, None:end of edit_text
        :type edit_pos: int
        :param layout: defaults to a shared :class:`StandardTextLayout` instance
        :type layout: text layout instance
        :param mask: hide text entered with this character, None:disable mask
        :type mask: bytes or unicode

        >>> Edit()
        <Edit selectable flow widget '' edit_pos=0>
        >>> Edit("Y/n? ", "yes")
        <Edit selectable flow widget 'yes' caption='Y/n? ' edit_pos=3>
        >>> Edit("Name ", "Smith", edit_pos=1)
        <Edit selectable flow widget 'Smith' caption='Name ' edit_pos=1>
        >>> Edit("", "3.14", align='right')
        <Edit selectable flow widget '3.14' align='right' edit_pos=4>
        """

        self.__super.__init__("", align, wrap, layout)
        self.multiline = multiline
        self.allow_tab = allow_tab
        self._edit_pos = 0
        self.set_caption(caption)
        self.set_edit_text(edit_text)
        if edit_pos is None:
            edit_pos = len(edit_text)
        self.set_edit_pos(edit_pos)
        self.set_mask(mask)
        self._shift_view_to_cursor = False

最佳答案

您不使用这些变量,所以只是盲目地传递它们。

class MyEdit(urwid.Edit):

    def __init__(self, my_attribute, *args, **kw):
        super().__init__(*args, **kw)
        self.my_attribute = []    

    def my_method(self):
        #some code that modifies my_attribute
        return self.my_attribute

关于python - 在 python 3 中从父类扩展 __init__ 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49835256/

相关文章:

php - 调用函数 - 变量

python - 计算第一次出现和个体出现之间的差异(Python Pandas)

python - 如何让 mypy 识别较新版本的 python?

python - 如何在 python 中访问 Facebook 墙提要?

python - 获取&符号之间或末尾的字符串

python - 我怎样才能简化这个 python 迭代?

Java 嵌套忽略内部属性

java - 枚举默认方法

python - 在 Python 中循环遍历 JSON 数组

python - 为什么 Tkinter 几何需要字符串?