I am not even sure if I have posed the problem with correct nomenclature, so please do correct me if I have misstated the question.
I wish to have a generic class that used to handle all subclass use cases, implementing methods depending on which subclass is being used. The example I will use here is easy enough to understand with the exact type of implementation I am using:
class display():
def __init__(self, **config):
self.type = config['type']
if self.type == 'type_a':
# Call constructor to type_a?
elif self.type == 'type_b':
# Call constructor to type_b?
def a_multi_use_function(self, **kwargs):
# Do something the same for all display types
print('hello! You have initialized a display of type {}'.format(self.type))
class type_a():
__init__(self, **config):
# implementation specific hardware configuration here
# Load drivers as necessary, override base class methods
# for device configuration
print('Hello! You are initializing device type_a, with config {}'.format(config)
def print_to_display(self, text):
# print to display the way you would on display a
print("printing on display a")
# Appropriate mechanics here.
class type_b():
__init__(self, **config):
# implementation specific hardware configuration here
# Load drivers as necessary, override base class methods
# for device configuration
print('Hello! You are initializing device type_b, with config {}'.format(config)
def print_to_display(self, text):
# print to display the way you would on display b
print("printing on display b")
# Appropriate mechanics here.
Usage:
config = {'type':'type_a'}
my_display = display(**config)
my_display.print_to_display('Hello world')
This should print the print_to_display message, depending on which type is passed.
Now, I know I could do this the way of making type_a and type_b as children, but I would like the calling code to be completely agnostic to what drivers/displays are available, so I could, for example, change a config file, add drivers, but always instantiate using display() instead of type_a() or type_b(). It also seems like, depending on other config parameters, I would want to override a set of methods. So, if a display device is of type_a, I would pull in hardware drivers for that. But if other hardware required different drivers, I could then instantiate that to override or add other methods.
Another way to do this could be just adding a short helper function like:
def get_display(**config):
if config['type'] == 'type_a':
return type_a(**config)
elif config['type'] == 'type_b':
return type_b(**config)
Using typical inheritance:
class type_b(display):
...
class type_a(display):
...
But again, as I mentioned above, if I have a device with potentially display type_a but the underlying device is type_1, I can see the case of wanting to pull in sets of methods into a superclass that shares a lot of common methods and properties.
Not sure if I'm missing something obvious here. TIA.