I have some code I am looking to implement in a more elegant way than my instincts want to do. I will do my best to describe what I am attempting.
class Fruit():
pass
class Apple(Fruit):
pass
class Orange(Fruit):
pass
def create_fruit(fruit_type):
test = ???? # code here to create instance of fruit of desired type called test
Ok, so hopefully this code makes some kind of sense. I have a function in a module that takes a bunch of parameters to create an instance of a class. I would ideally like to pass a parameter stating what type of class to create (but they would all be instances or subclasses of the same superclass). The parameters for each subclass would be the same (as of now).
I could probably do something with if statements pretty easily and hacked together (something like, if fruit_type==1
, test=Apple()
, if fruit_type == 2
, test=Orange()
, etc…), but in trying to improve as a python programmer, I wanted to know if there was a better way of doing this. I have briefly read on decorators and functional programming (though it is still quite abstract to me, and will take a little more time to wrap my head around), so perhaps this is in that same vein?