0

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??

3
  • There's no such thing as an "inner class" in Python. You don't typically need any. Commented Jul 27, 2018 at 12:40
  • Why do you need inner class at all? What are you trying to achieve? Do you want Farm to be a container of Animal objects or do you want it to be base class of Animal? Or something else? Anyway... your code would work if you put line my_pet_cat = Animal("Meeeeooowww", "Fluffy") after definition of Animal. Commented Jul 27, 2018 at 12:42
  • I am just hiding this stuff away. Farm is going to be available to the rest of the application - but Animal and other inner classes I want to be more "hidden", so I am encapsulating them within Farm. In my actual application it is used for UI Menus and Menu Items. Commented Jul 27, 2018 at 12:48

2 Answers 2

3

Define Animal class before you reference it to create an instance.

class Farm:
    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

    my_pet_cat = Animal("Meeeeooowww", "Fluffy")
Sign up to request clarification or add additional context in comments.

4 Comments

this answers the actual question, but it's quite likely that my_pet_cat should be an instance variable instantiated in __init__ rather than a class variable.
@MatthewStory, in my case it's fine. I want it to be a singleton kind of pattern.
@ChickenFeet this is a pretty weird way to do a singleton in python. The standard approches to doing that are covered here stackoverflow.com/questions/31875/… ... the top 3 answers are what you typically see.
@MatthewStory okay thanks, I'll have a look :) I am trying to force a bit more structure into python, coming from C#.
0

Why not use a module named Farm and in the Farm module, define a class name Animal?

1 Comment

The module is farm which has a class Farm. I am trying to hide away the classes in Farm so the rest of the application doesn't get crowded. Perhaps there isn't a need for this, since you can import individual classes from a module - but it's done now :p

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.