0

Problem Statement:

Consider some values as:

Fruits ---> Apples(Red, (3,2), Organic), Oranges(Orange, (5,2), Non-Organic) and so on ...

I want to define a Parent Class as Fruits and then within this Parent class want these Objects with multiple values defined.

Then if the conditions match and Class Oranges got created, I want to run a specific function which is only for the class Oranges.

I am new to such complex programming in Python.

Open to suggestions as well !

3
  • What do the tuples mean?, Also I presume you want Python 2.x? Commented Aug 12, 2018 at 23:34
  • Just an example, Wanted to show the hierarchy of how the values are ... Python 2.x Commented Aug 12, 2018 at 23:39
  • Could you give an example of what you have tried so far and the problems you are encountering? Commented Aug 13, 2018 at 1:51

2 Answers 2

0

Your question is really ambiguous.

You say you want the parent class Fruits to contain objects of type Orange/Apple etc.But you also say depending on what class got created you wanted to do something.

*If the conditions match... . (What conditions??) you haven't specified what conditions. Based on what you've provided I have an interpretation of what the answer should be.

class Fruit(object):
    color = None
    values = None
    nature = None

    def __init__(self, color, values, nature):
        self.color = color
        self.values = values
        self.nature = nature

class Orange(Fruit):
    color = "Orange"

    def __init__(self, values, nature):
        super(Orange, self).__init__(self.color, values, nature)

class Apple(Fruit):
    color = "Red"

    def __init__(self, values, nature):
        super(Apple, self).__init__(self.color, values, nature)



# a = Fruit("Green", (3,4), "Organic")
l = []
l.append(Fruit("Green", (3,4), "Organinc"))
l.append(Orange((3,4), "Non Organic"))
l.append(Apple((4,3), "Organic"))

print l

for f in l:
    if type(f) is Orange:
        print "Found an orange"
Sign up to request clarification or add additional context in comments.

Comments

0

Looks like you need to use multiple inheritance?

class Fruits(object):
    def __init__(self, fruit):
        print fruit + "is a fruit"

class Organic(Fruits):
    def __init__(self, fruit):
        print fruit + "is organic"
        super(Organic, self).__init__(fruit)

class Colored(Organic):
    def __init__(self, fruit, color):
        print fruit + "is " + color
        super(Colored, self).__init__(fruit)

class Apple(Colored, Organic):
    def __init__(self):
        super(Apple, self).__init__("apple", "red")

apple = Apple()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.