I want to be able to instance an object and interact with it without knowing what the specific type of that object will be at the onset. Basically, I want to have the object take on the properties specified say in a config file, but then interact with it using a uniform handle.
I'm trying to decide whether I need to use Python inheritance or nested/inner classes for the following example.
For example, I would like to do the following
config.animal_type = 'Squirrel'
a = Animal()
a.walk()
I'm walking on 4 legs!
config.animal_type = 'Monkey'
b = Animal()
b.walk()
I'm walking on 2 legs!
The class structure would look similar to what I have below, but I would use the config.animal_type variable to decide what kind of type a
& b
would be.
class Animal:
def __init__(self)
...
def walk(self)
...
def eat(self)
...
class Squirrel(Animal):
def walk(self):
self.walk_on_4_legs()
super().walk()
def walk_on_4_legs(self):
print("I'm walking on 4 legs!")
class Monkey(Animal):
def walk(self):
self.walk_on_2_legs()
super(Monkey, self).walk()
def walk_on_2_legs(self):
print("I'm walking on 2 legs!")
Alternatively, I've also been investigating using nested/inner classes, where I would only define Squirrel
and Monkey
classes inside the Animal
class, and then declare the object only based on the config variable. The only thing is I've read that nested/inner classes aren't advisable as a best practice.
I would appreciate clarification as to the best method to implement this functionality.