Multiple Inheritance in Python where the second class overrides methods from the first class

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












I try to write a wrapper for all widgets that contain text that needs to be translated for wxPython and make them handle the initial translation and the change of language on their own using gettext.



I already have a working prototype but I'm sure this is not the correct way to do it. Here is an example for all subclasses of wx.Control.



My Idea is to override the relevant methods of wx.Control and then merge the actual widgets (subclasses of wx.Control) together with my Control.



import wx
from wx.lib.pubsub import pub

class Control(wx.Control):
def __init__(self, label):
self.ml_label = label
pub.subscribe(self.Update, 'language.changed')

def Update(self):
super().Update()
super().SetLabel(_(self.ml_label))

def SetLabel(self, label):
self.ml_label = label
super().SetLabel(_(self.ml_label))


class Button(wx.Button, Control):
def __init__(self, parent, id=wx.ID_ANY, label=wx.EmptyString, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0,
validator=wx.DefaultValidator, name=wx.ButtonNameStr):
Control.__init__(self, label)
wx.Button.__init__(self, parent, id, _(label), pos, size, style, validator, name)


class StaticText(wx.StaticText, Control):
def __init__(self, parent, id=wx.ID_ANY, label=wx.EmptyString, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0,
name=wx.StaticTextNameStr):
Control.__init__(self, label)
wx.StaticText.__init__(self, parent, id, _(label), pos, size, style, name)


Before I had the code from the Control class in Button and StaticText but as there are lots of widgets based on Control I wanted to move the duplicate code to one place.



It works, but I'm sure it's not the right way to do it especially as I don't call super().__init__ in the Control class, which can't be right.



Should I even be using multiple inheritance for what I am trying to do here?







share|improve this question



























    up vote
    4
    down vote

    favorite












    I try to write a wrapper for all widgets that contain text that needs to be translated for wxPython and make them handle the initial translation and the change of language on their own using gettext.



    I already have a working prototype but I'm sure this is not the correct way to do it. Here is an example for all subclasses of wx.Control.



    My Idea is to override the relevant methods of wx.Control and then merge the actual widgets (subclasses of wx.Control) together with my Control.



    import wx
    from wx.lib.pubsub import pub

    class Control(wx.Control):
    def __init__(self, label):
    self.ml_label = label
    pub.subscribe(self.Update, 'language.changed')

    def Update(self):
    super().Update()
    super().SetLabel(_(self.ml_label))

    def SetLabel(self, label):
    self.ml_label = label
    super().SetLabel(_(self.ml_label))


    class Button(wx.Button, Control):
    def __init__(self, parent, id=wx.ID_ANY, label=wx.EmptyString, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0,
    validator=wx.DefaultValidator, name=wx.ButtonNameStr):
    Control.__init__(self, label)
    wx.Button.__init__(self, parent, id, _(label), pos, size, style, validator, name)


    class StaticText(wx.StaticText, Control):
    def __init__(self, parent, id=wx.ID_ANY, label=wx.EmptyString, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0,
    name=wx.StaticTextNameStr):
    Control.__init__(self, label)
    wx.StaticText.__init__(self, parent, id, _(label), pos, size, style, name)


    Before I had the code from the Control class in Button and StaticText but as there are lots of widgets based on Control I wanted to move the duplicate code to one place.



    It works, but I'm sure it's not the right way to do it especially as I don't call super().__init__ in the Control class, which can't be right.



    Should I even be using multiple inheritance for what I am trying to do here?







    share|improve this question























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I try to write a wrapper for all widgets that contain text that needs to be translated for wxPython and make them handle the initial translation and the change of language on their own using gettext.



      I already have a working prototype but I'm sure this is not the correct way to do it. Here is an example for all subclasses of wx.Control.



      My Idea is to override the relevant methods of wx.Control and then merge the actual widgets (subclasses of wx.Control) together with my Control.



      import wx
      from wx.lib.pubsub import pub

      class Control(wx.Control):
      def __init__(self, label):
      self.ml_label = label
      pub.subscribe(self.Update, 'language.changed')

      def Update(self):
      super().Update()
      super().SetLabel(_(self.ml_label))

      def SetLabel(self, label):
      self.ml_label = label
      super().SetLabel(_(self.ml_label))


      class Button(wx.Button, Control):
      def __init__(self, parent, id=wx.ID_ANY, label=wx.EmptyString, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0,
      validator=wx.DefaultValidator, name=wx.ButtonNameStr):
      Control.__init__(self, label)
      wx.Button.__init__(self, parent, id, _(label), pos, size, style, validator, name)


      class StaticText(wx.StaticText, Control):
      def __init__(self, parent, id=wx.ID_ANY, label=wx.EmptyString, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0,
      name=wx.StaticTextNameStr):
      Control.__init__(self, label)
      wx.StaticText.__init__(self, parent, id, _(label), pos, size, style, name)


      Before I had the code from the Control class in Button and StaticText but as there are lots of widgets based on Control I wanted to move the duplicate code to one place.



      It works, but I'm sure it's not the right way to do it especially as I don't call super().__init__ in the Control class, which can't be right.



      Should I even be using multiple inheritance for what I am trying to do here?







      share|improve this question













      I try to write a wrapper for all widgets that contain text that needs to be translated for wxPython and make them handle the initial translation and the change of language on their own using gettext.



      I already have a working prototype but I'm sure this is not the correct way to do it. Here is an example for all subclasses of wx.Control.



      My Idea is to override the relevant methods of wx.Control and then merge the actual widgets (subclasses of wx.Control) together with my Control.



      import wx
      from wx.lib.pubsub import pub

      class Control(wx.Control):
      def __init__(self, label):
      self.ml_label = label
      pub.subscribe(self.Update, 'language.changed')

      def Update(self):
      super().Update()
      super().SetLabel(_(self.ml_label))

      def SetLabel(self, label):
      self.ml_label = label
      super().SetLabel(_(self.ml_label))


      class Button(wx.Button, Control):
      def __init__(self, parent, id=wx.ID_ANY, label=wx.EmptyString, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0,
      validator=wx.DefaultValidator, name=wx.ButtonNameStr):
      Control.__init__(self, label)
      wx.Button.__init__(self, parent, id, _(label), pos, size, style, validator, name)


      class StaticText(wx.StaticText, Control):
      def __init__(self, parent, id=wx.ID_ANY, label=wx.EmptyString, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0,
      name=wx.StaticTextNameStr):
      Control.__init__(self, label)
      wx.StaticText.__init__(self, parent, id, _(label), pos, size, style, name)


      Before I had the code from the Control class in Button and StaticText but as there are lots of widgets based on Control I wanted to move the duplicate code to one place.



      It works, but I'm sure it's not the right way to do it especially as I don't call super().__init__ in the Control class, which can't be right.



      Should I even be using multiple inheritance for what I am trying to do here?









      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 25 at 11:41
























      asked Mar 23 at 14:35









      Michael K.

      1212




      1212




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          After I posted this question and saw that I wrote that I try to write a Wrapper, I thought that this might be the solution...



          import wx
          from wx.lib.pubsub import pub

          def multilingual(wrapped):

          class WrappedClass(wrapped):
          def __init__(self, *args, **kwargs):
          # Translate label parameter
          if len(args) >= 3:
          self.ml_label = args[2]
          args = list(args)
          args[2] = _(self.ml_label)
          else:
          self.ml_label = kwargs.get('label',wx.EmptyString)
          kwargs['label'] = _(self.ml_label)
          super().__init__(*args,**kwargs)
          pub.subscribe(self.Update, 'language.changed')

          def Update(self):
          print('update')
          super().Update()
          super().SetLabel(_(self.ml_label))

          def SetLabel(self, label):
          print('setlabel')
          self.ml_label = label
          super().SetLabel(_(self.ml_label))

          return WrappedClass

          @multilingual
          class Button(wx.Button):
          pass

          @multilingual
          class StaticText(wx.StaticText):
          pass





          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190309%2fmultiple-inheritance-in-python-where-the-second-class-overrides-methods-from-the%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            After I posted this question and saw that I wrote that I try to write a Wrapper, I thought that this might be the solution...



            import wx
            from wx.lib.pubsub import pub

            def multilingual(wrapped):

            class WrappedClass(wrapped):
            def __init__(self, *args, **kwargs):
            # Translate label parameter
            if len(args) >= 3:
            self.ml_label = args[2]
            args = list(args)
            args[2] = _(self.ml_label)
            else:
            self.ml_label = kwargs.get('label',wx.EmptyString)
            kwargs['label'] = _(self.ml_label)
            super().__init__(*args,**kwargs)
            pub.subscribe(self.Update, 'language.changed')

            def Update(self):
            print('update')
            super().Update()
            super().SetLabel(_(self.ml_label))

            def SetLabel(self, label):
            print('setlabel')
            self.ml_label = label
            super().SetLabel(_(self.ml_label))

            return WrappedClass

            @multilingual
            class Button(wx.Button):
            pass

            @multilingual
            class StaticText(wx.StaticText):
            pass





            share|improve this answer

























              up vote
              0
              down vote













              After I posted this question and saw that I wrote that I try to write a Wrapper, I thought that this might be the solution...



              import wx
              from wx.lib.pubsub import pub

              def multilingual(wrapped):

              class WrappedClass(wrapped):
              def __init__(self, *args, **kwargs):
              # Translate label parameter
              if len(args) >= 3:
              self.ml_label = args[2]
              args = list(args)
              args[2] = _(self.ml_label)
              else:
              self.ml_label = kwargs.get('label',wx.EmptyString)
              kwargs['label'] = _(self.ml_label)
              super().__init__(*args,**kwargs)
              pub.subscribe(self.Update, 'language.changed')

              def Update(self):
              print('update')
              super().Update()
              super().SetLabel(_(self.ml_label))

              def SetLabel(self, label):
              print('setlabel')
              self.ml_label = label
              super().SetLabel(_(self.ml_label))

              return WrappedClass

              @multilingual
              class Button(wx.Button):
              pass

              @multilingual
              class StaticText(wx.StaticText):
              pass





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                After I posted this question and saw that I wrote that I try to write a Wrapper, I thought that this might be the solution...



                import wx
                from wx.lib.pubsub import pub

                def multilingual(wrapped):

                class WrappedClass(wrapped):
                def __init__(self, *args, **kwargs):
                # Translate label parameter
                if len(args) >= 3:
                self.ml_label = args[2]
                args = list(args)
                args[2] = _(self.ml_label)
                else:
                self.ml_label = kwargs.get('label',wx.EmptyString)
                kwargs['label'] = _(self.ml_label)
                super().__init__(*args,**kwargs)
                pub.subscribe(self.Update, 'language.changed')

                def Update(self):
                print('update')
                super().Update()
                super().SetLabel(_(self.ml_label))

                def SetLabel(self, label):
                print('setlabel')
                self.ml_label = label
                super().SetLabel(_(self.ml_label))

                return WrappedClass

                @multilingual
                class Button(wx.Button):
                pass

                @multilingual
                class StaticText(wx.StaticText):
                pass





                share|improve this answer













                After I posted this question and saw that I wrote that I try to write a Wrapper, I thought that this might be the solution...



                import wx
                from wx.lib.pubsub import pub

                def multilingual(wrapped):

                class WrappedClass(wrapped):
                def __init__(self, *args, **kwargs):
                # Translate label parameter
                if len(args) >= 3:
                self.ml_label = args[2]
                args = list(args)
                args[2] = _(self.ml_label)
                else:
                self.ml_label = kwargs.get('label',wx.EmptyString)
                kwargs['label'] = _(self.ml_label)
                super().__init__(*args,**kwargs)
                pub.subscribe(self.Update, 'language.changed')

                def Update(self):
                print('update')
                super().Update()
                super().SetLabel(_(self.ml_label))

                def SetLabel(self, label):
                print('setlabel')
                self.ml_label = label
                super().SetLabel(_(self.ml_label))

                return WrappedClass

                @multilingual
                class Button(wx.Button):
                pass

                @multilingual
                class StaticText(wx.StaticText):
                pass






                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Mar 23 at 16:20









                Michael K.

                1212




                1212






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190309%2fmultiple-inheritance-in-python-where-the-second-class-overrides-methods-from-the%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation