I have a static base class, which I want to encapsulate child classes. I cannot find the syntax to create the inner classes from within a static outer class.
Here's an example of what I want:
class Farm:
my_pet_cat = Animal("Meeeeooowww", "Fluffy")
class Animal:
def __init__(self, sound, fur):
self.sound = sound
self.fur = fur
def speak(self):
print(self.sound)
def pet(self):
return self.fur
NameError: name 'Animal' is not defined
I tried accessing Animal with self.Animal(...) but this didn't work, as obviously Farm doesn't have a self, being a static class and all. I also successfully accessed Animal if it is placed outside of Farm, but I want to encapsulate the Animal class within the Farm class.
Can this be done??
Farmto be a container ofAnimalobjects or do you want it to be base class ofAnimal? Or something else? Anyway... your code would work if you put linemy_pet_cat = Animal("Meeeeooowww", "Fluffy")after definition ofAnimal.Farmis going to be available to the rest of the application - butAnimaland other inner classes I want to be more "hidden", so I am encapsulating them withinFarm. In my actual application it is used for UI Menus and Menu Items.