Im having trouble finding an efficient solution to a Python inheritance problem.
So there exists some code like the following:
class Foo( object ):
def __init__():
self.some_vars
def foo_func():
x = Bar()
def foo_bar_func()
x += 1
class Fighters( Foo ):
def __init__():
Foo.__init__()
def func1():
Foo.some_attribute
def func2():
Foo.someother_func()
class Bar():
def bar_func():
#some stuff here
The problem lies in that I need to override Bar.bar_func()
but that is two levels deep. I solved it by doing the following:
class Foo( Foo ):
def __init__():
Foo.__init__()
def foo_func(): #Overridden method
x = myBar()
class myFighters( Foo ):
def __init__():
Foo.__init__()
def func1():
Foo.some_attribute
def func2():
Foo.someother_func()
class myBar():
def bar_func(): #the function that I actually need to change
#my sweet code here
The only thing that is actually different is myBar.bar_func()
but I had to do at least 2 things that I think is ugly.
One is that I had to create a class Foo
that inherits from Foo
. This seems like an odd thing to do and doesnt leave things very clear. I did this to save me from having to rename every reference in myFighters
from Foo
to myFoo
.
The second is that I have to copy all of the code from Fighters
into myFighters
for the sole purpose of using an overridden function in Bar()
. Fighters
and myFighters
are exactly the same, except that Fighters
uses the Foo
that calls Bar()
and myFighters
uses the Foo
that calls myBar()
. Does anyone have any suggestions to fix those two issues? Or should I just be thankful I found a solution and move on with my life...