Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correct way to extend __init__ in python 3 from parent class

General Question: What is the easiest/"most pythonic" way to initialize a child class exactly as its parent, but adding a single attribute?

My specific question: I'd like to extend an (Urwid) Edit object to include a single additional attribute, my_attribute; I've copied the original signature into __init__ and super().__init__, but there are a few undefined parameters/constants (LEFT, SPACE) in the signature and I don't understand how they're set in the parent class. Below are my (breaking) class definition and the parent init method:

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
like image 591
anon01 Avatar asked Apr 14 '18 19:04

anon01


People also ask

How do you call a parent init class?

Use super(). __init__() to call the immediate parent class constructor. Call super(). __init__(args) within the child class to call the constructor of the immediate parent class with the arguments args .

What is super () __ Init__?

When you initialize a child class in Python, you can call the super(). __init__() method. This initializes the parent class object into the child class. In addition to this, you can add child-specific information to the child object as well.

How do you extend one class to another in Python?

Use the inheritance syntax class DerivedClass(BaseClass) to extend a class. Use the syntax class DerivedClass(BaseClass) to extend BaseClass as DerivedClass , so that DerivedClass inherits BaseClass .

Can you call parent class without its instance creation in Python?

Well this can done using Python. You just have to create an object of the child class and call the function of the parent class using dot(.) operator.


1 Answers

You don't use those variables so just blindly pass them through.

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
like image 86
tdelaney Avatar answered Sep 17 '22 17:09

tdelaney