Reputation: 445
Right now, my project has the following structure :
main.py
-------
class main_fun(object):
def __init__(self, <parameters>):
ops.py
------
class ops_fun(main_fun):
def __init__(self):
super(ops_fun, self).__init__(<parameters>)
It essentially translates to the following :
------------------
main_fun (main.py)
------------------
|
|
----------------
ops_fun (ops.py)
----------------
I would like to split/restructure the above into the following :
------------------
main_fun (main.py)
------------------
/ | \
/ | \
---------------- ---------------- ----------------
AuxOps (aops.py) === CoreOps (cops.py) === DerOps (dops.py)
---------------- ---------------- ----------------
\ | /
\ | /
------------------
con_fun (contf.py)
------------------
Which basically means that I want to :
main_fun
to each of AuxOps
, CoreOps
, DerOps
and con_fun
.AuxOps
, CoreOps
and DerOps
, should be inherited in each others' classes. i.e., AuxOps
should inherit every method in CoreOps
and DerOps
, DerOps
should inherit every method in CoreOps
and AuxOps
.AuxOps
, CoreOps
and DerOps
in con_fun
(Does inheriting these automatically inherit main_fun
, since it is the parent of these?).How can I achieve the above?
Upvotes: 0
Views: 59
Reputation: 8222
An opinionated answer. I'd recommend composing using a base class and plug-ins. Multiple inheritance of general classes can be an utter pain to debug.
class AuxPlugin( object):
def auxMethod1( self, ...)
...
class CorePlugin( object)
def coreMethod1( self, ...)
...
class DerPlugin( object)
def derMethod1( self, ...)
...
class AuxOps( AuxPlugin, Main_Fun): # better, call Main_Fun Base_whatever
pass
class CoreOps( CorePlugin, Main_Fun):
pass
class DerOps( DerPlugin, Main_Fun):
pass
class ConFun( AuxPlugin, CorePlugin, DerPlugin, Main_Fun ):
pass
# and possible
# class CoreDerOps( CorePlugin, DerPlugin, Main_Fun):
# pass
# etc. Mix'n'match.
The "rules" are:
object
, and do not do anything except define ordinary methods. You can repeat this pattern multiple levels deep:
class FooFun( FooPlugin, ConFun):
# gets all of ConFun's plug-in methods, its base class, and added FooPlugin's methods
One level down, it is permissible to have plugged-in methods that intercept and augment methods inherited from a lower level, calling super().name()
where appropriate.
The key thing is that finding out what refers to what is easy. If it's in a Plugin, there is no superclass to worry about. Any calls to super()
refer to the base class, or further up its inheritance tree.
Upvotes: 2
Reputation: 29099
Python allows multiple inheritance, so you can directly
class Parent ...
class ChildA(Parent) ...
class ChildB(Parent)...
class Grandchild(ChildA, ChildB)...
The child inherits all of its parents methods. This causes the problem that the Grandchild
will inherit the methods of the Parent
class through both of the parents. By default, when asked to call the method, python will look at the leftmost parent class first.
For more details, look up diamond problem and python's method resolution order.
Upvotes: 3