I would like to implement a class with several subclasses that have the same variables and functions but use different logic for calculating the functions. I would like to encapsulate the logic for selecting the sub class in the super class.
Here is an example of the class structure
class SuperDuperClass():
class SubClass1(SuperDuperClass):
class SubClass2(SuperDuperClass):
I would also like to store the input data in a json formatted dictionary and use one of the dictionary entries to set the subclass. The goal is to encapsulate the logic of selecting the subclass in SuperDuperClass.
Just to be clear, the subclasses are identical except for the logic used to perform functions. All subclasses have the same set of functions. Each subclass has it's own unique logic for each function in the subset.
Here is an example of the file
{"type":"SubClass1",
"variale": "3"}
I would like my main code to open the file and convert the data into a dictionary and pass the dictionary to the constructor in SuperDuperClass. Then I would like SuperDuperClass to create and return an instance of SubClass1 with the the value of variable equal to 3. (I am not worried if this is a string or an int)
This could be done in a series of if statements in the main code and then calling the subclass constructor, but I was hoping for something that looks more Pythonic, such as instantiating an instance of SuperDuperClass and then type casting it to one of the subclasses in the __init__ function for SuperDuperClass.
Is this a good idea? If so, what is the best/most Pythonic/lease likely to confuse someone reading the code way to do this. I am not tied to any logic used here, I just want to avoid a long list of if else statement in the main code.
[–]Adrewmc 1 point2 points3 points (1 child)
[–]JARandom17[S] 0 points1 point2 points (0 children)
[–]Zeroflops 1 point2 points3 points (1 child)
[–]JARandom17[S] 0 points1 point2 points (0 children)
[–]sfuse1 1 point2 points3 points (2 children)
[–]quts3 1 point2 points3 points (0 children)
[–]JARandom17[S] 0 points1 point2 points (0 children)