skrowten_hermit
skrowten_hermit

Reputation: 445

Classses, inheritance and methods/functions in Python

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 :

  1. inherit all methods/functions and variables from the class main_fun to each of AuxOps, CoreOps, DerOps and con_fun.
  2. have different methods/functions implemented in each of 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.
  3. inherit each of 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

Answers (2)

nigel222
nigel222

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:

  1. Plugins always inherit from object, and do not do anything except define ordinary methods.
  2. Plugins all go to the left of the single base class in classes inheriting the base class
  3. 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)
  4. 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.

Upvotes: 2

blue note
blue note

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

Related Questions