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 :
- inherit all methods/functions and variables from the class
main_funto each ofAuxOps,CoreOps,DerOpsandcon_fun. - have different methods/functions implemented in each of
AuxOps,CoreOpsandDerOps, should be inherited in each others' classes. i.e.,AuxOpsshould inherit every method inCoreOpsandDerOps,DerOpsshould inherit every method inCoreOpsandAuxOps. - inherit each of
AuxOps,CoreOpsandDerOpsincon_fun(Does inheriting these automatically inheritmain_fun, since it is the parent of these?).
How can I achieve the above?
-
1I'm quite new to multi-inheritance, but can you try this : ```` class con_fun(AuxOps,CoreOps,DerOps): pass ````Born Tbe Wasted– Born Tbe Wasted2019年03月28日 10:49:38 +00:00Commented Mar 28, 2019 at 10:49
2 Answers 2
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.
2 Comments
ChildA, can I do class ChildA(Parent, ChildB, ChildC) so that all the methods of ChildB and ChildC are inherited to it. And likewise for ChildB and ChildC? If yes, is it a good practice?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:
- Plugins always inherit from
object, and do not do anything except define ordinary methods. - Plugins all go to the left of the single base class in classes inheriting the base class
- Plugins should have their own unique set of methods that do not overlap with other compatible plugins (but if they do, the leftmost plugin wins)
- If you break these rules you will regret it later when you forget you did, or some other poor sod will curse you and damage your karma.
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.
4 Comments
self.A declared and updated in grandchild class, would it be available in a method in the child class as well as parent class? (I haven't tried this in even a regular hierarchy!)self refers to the instance and all the class / subclass code shares that instance.Explore related questions
See similar questions with these tags.