python初始化父类_从父类扩展python 3中的__init__的正确方法

一般问题:完全像父类一样初始化子类,但添加单个属性的最简单/“最pythonic”的方法是什么?

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

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("Y/n? ", "yes")

>>> Edit("Name ", "Smith", edit_pos=1)

>>> Edit("", "3.14", align='right')

"""

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值