Multiple Inheritance in Python where the second class overrides methods from the first class
Clash 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?
python python-3.x inheritance wxpython
add a comment |Â
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?
python python-3.x inheritance wxpython
add a comment |Â
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?
python python-3.x inheritance wxpython
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?
python python-3.x inheritance wxpython
edited Apr 25 at 11:41
asked Mar 23 at 14:35
Michael K.
1212
1212
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Mar 23 at 16:20
Michael K.
1212
1212
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password