-1

What is best practice in Python for creating a class that wraps many different types of classes with the same base class?

In my specific situation, I have a base class (1st layer) that represents a generalized data feed, and multiple classes (2nd layer) that inherit from this base class that represent different types of data sources. I would like to create a class that inherits from either the 1st or 2nd layer, and performs some task on the data coming from one of the sources defined in the 2nd layer. I am using a library, so cannot change this structure between these two layers of classes.

The two ways I was thinking of doing this was to either:

  1. Create a MetaClass where I can create a Class at runtime that will inherit dynamically from one of the 2nd layer of classes. This way it will automatically use the methods of its base class in the 2nd layer. Although how do I keep it from creating an instance of the MetaClass when I define the Class itself? And how do I make the MetaClass to set the superclass of the Class?
  2. Create a class that inherits from the 1st layer, has a member variable that's an instance of 2nd layer class. Although, I would need to create a method for every one that's define in the 2nd layer and calls it.
Kamil
  • 11
  • 3
  • I wonder [why](https://en.wikipedia.org/wiki/Composition_over_inheritance) do you want to solve it using complex inheritance. I'd rather factor out the second layer classes, and passed them to an instance of the first class that need specific data source access. – 9000 May 03 '18 at 16:44
  • This looks very vague to me. Some code (meaningful code, please, not foobar classes and methods) would probably help. – Stop harming Monica May 03 '18 at 16:54

1 Answers1

0

Personally, I'll prefer the second option. Python motto is "Explicit is better than implicit" so I think it's better to manually route the calls to the various methods onto the methods of the member object.

Of course, if there are a lot of methods, it might be interresting to automatically route the methods...

Olivier CAYROL
  • 256
  • 2
  • 5