Skip to content
Advertisement

Class that can inherit from two different classes [closed]

So I am working on an implementation of a Dynamic Decision Network (DDN) class. This DDN is a type of Bayesian Network (BN) with some additional functionality.

However, I also defined a Quantum Bayesian Network (QBN), that uses quantum computing to perform inference. Its implementation is very similar to the BN class, I only change one of the BN methods to perform quantum computations instead (the query method) and add some other methods just to help perform these computations.

I want the DDN class to be able to inherit from the BN class if one wants to perform the computations classically, and inherit from the QBN class if one wants to perform the computations in a quantum manner.

The code looks something like this:

class BN:
   ...
   def query(self, ...):
       ...

class QBN(BN):
   ...
   def query(self, ...):
      # Modified query method
      ...

class DDN(???):
   ...

Am I structuring the hierarchy wrong in some way? How can I reconcile this?

Advertisement

Answer

You can put the class into a factory function:

def make_DDN(Base):
    class DDN(Base):
       def other_methods(self):
           ...

    return DDN

Now you can make new classes:

DDN1 = make_DDN(BN)

etc

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement