0

I am not sure how to accomplish something that should be straightforward.

I want to define a class. In that class will be subclasses. In those subclasses will be attributes.

Ok, that's easy. But I want the attributes of one subclass to be generated based on the attributes of another of the subclasses. Here is my (wrong) code to try to do that:

class Food(object):
    class Fruits(object):
        crunchy=['Apples', 'Pears']
        juicy=['Limes', 'Lemons']

    class Salads(object):
        import Fruits
        FruitSalad=crunchy+juicy+['Whipped Cream']

Obviously "import Fruits" is wrong here. But how can I accomplish what I need?

-------------edit/addendum--------------------------------------

Ok, so I lose the outer class "Food", that's ok, I never liked it anyway.

I can now instantiate the first class into the second like this:

class Fruits(object):
    crunchy=['Apples', 'Pears']
    juicy=['Limes', 'Lemons']

class Salads(object):
    fruit=Fruits
    FruitSalad=fruit.crunchy+fruit.juicy+['Whipped Cream']

Which is closer, but I really want to lose the "fruit." structure.

9
  • Why are you nesting classes? What are you trying to accomplish? Commented Feb 12, 2013 at 15:59
  • I want to be able to instantiate "Food.Salads" somewhere else. Commented Feb 12, 2013 at 16:00
  • 1
    You do not need to use import Fruits at all. Only use import to import structures from another file. These definitions are in the same file. Commented Feb 12, 2013 at 16:00
  • 1
    Why not just create a module named food, then you can do import food and create an instance of food.Salads(). Commented Feb 12, 2013 at 16:01
  • Yeah, I realize it is wrong. I put it there to illustrate what I was trying to do. Commented Feb 12, 2013 at 16:01

2 Answers 2

3

Perhaps there is some confusion about subclasses vs. encapsulation. Here is an example hierarchy if you're looking for inheritance. Note that this particular code is not making much use of the hierarchy and just some bare lists would be easier, and the variables are all class variables, not per-instance.

class Food(object):
    pass

class Fruit(Food):
    pass

class Salad(Food):
    pass

class CrunchyFruit(Fruit):
    ingredients = ['apples','pears']

class JuicyFruit(Fruit):
    ingredients = ['limes','lemons']

class FruitSalad(Salad):
    ingredients = JuicyFruit.ingredients + CrunchyFruit.ingredients + ['whipped cream']

print(FruitSalad.ingredients)
Sign up to request clarification or add additional context in comments.

Comments

1

Because you are defining this as a nested set of classes, you are needlessly complicating things. The explanation is now going to involve words like "class suite" and why you'd need Fruits.crunchy in one place and Food.Fruits.crunchy in another. You are also trying to concatenate lists and a string, which won't work.

Just keep things simple, create a module named food, then you can do import food and create an instance of food.Salads().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.