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 !

P.W
  • 63
  • 5

2 Answers2

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()
Ian Gabaraev
  • 101
  • 1
  • 8
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"
Srini
  • 1,619
  • 1
  • 19
  • 34