3 Replies - 165 Views - Last Post: 16 April 2013 - 12:56 PM Rate Topic: -----

#1 Static Hazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 25-November 09

Accessing other object's functions outside of parent class (basic)

Posted 16 April 2013 - 01:30 AM

This is a very basic problem, but I can't wrap my mind around it. I know what I need to do, I just can't figure out how.

I have two classes. One is Fruit, with various attributes, and one is FruitBasket, which holds the fruit. I want to add the fruit to the basket, which is very simple. However, I can't have more than one of each type of fruit in the basket. So in append, I need to iterate through the fruit basket, before appending. No problem, right?

For me...WRONG! I cannot figure out how to access Fruit's getType() function. It's really easy if they are combined in one file, but this is an extremely simplified version of a larger project I am working on, and all of the files are separated.

So lets say fruit.py is located in the Project1 package, in the production folder. So I would add the appropriate import statement in fruitbasket.py. No problem.

However, I have no idea how to access Fruit's getType() method in the comparison condition, where the list of fruit objects is being iterated through. Check below.

class Fruit(object):

    def __init__(self, typeOfFruit, number, color):
        self.theType = typeOfFruit
        self.theNumber = number
        self.theColor = color

    def getType(self):
        return self.theType



import Project1.production.fruit as fruit 

class FruitBasket(object):

    def __init__(self):
        #the basket is empty upon initialization
        self.theBasket = []
        
    def __iter__(self):     # make iterable
        return iter(self.theBasket)
    
    def addFruit(self, currentFruit):

    duplicate = False

        for item in self.theBasket:
           #HERE IS MY PROBLEM
           if(currentFruit.getType() == item.getType()):
               duplicate = True
               break

        if(duplicate == False):
           #add fruit
        else:
           #dont add fruit




Do I need to set currentFruit as a fruit object somehow? Do I need to add Project1.production.currentFruit or something? I am so confused. I have this same exact problem in the file with my unit tests...I cannot figure out how to use methods that aren't explicitly written in that class!

This post has been edited by Static Hazard: 16 April 2013 - 01:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Accessing other object's functions outside of parent class (basic)

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 3,032
  • Joined: 21-June 11

Re: Accessing other object's functions outside of parent class (basic)

Posted 16 April 2013 - 04:30 AM

There's nothing wrong with doing currentFruit.getType() exactly like you did. If you're encountering problems with that code, please tell us what those problems are.

View PostStatic Hazard, on 16 April 2013 - 10:30 AM, said:

Do I need to set currentFruit as a fruit object somehow?


When you call the addFruit method, you need to pass in a Fruit object as the argument. Other than that there's nothing special you need to do to make Python aware that currentFruit is supposed to be a Fruit object.
Was This Post Helpful? 0
  • +
  • -

#3 Static Hazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 25-November 09

Re: Accessing other object's functions outside of parent class (basic)

Posted 16 April 2013 - 11:29 AM

View Postsepp2k, on 16 April 2013 - 05:30 AM, said:

There's nothing wrong with doing currentFruit.getType() exactly like you did. If you're encountering problems with that code, please tell us what those problems are.

View PostStatic Hazard, on 16 April 2013 - 10:30 AM, said:

Do I need to set currentFruit as a fruit object somehow?


When you call the addFruit method, you need to pass in a Fruit object as the argument. Other than that there's nothing special you need to do to make Python aware that currentFruit is supposed to be a Fruit object.


How can I differentiate between a variable and an object though? I think my problem currently is that the addFruit(currentFruit) method thinks that currentFruit is a variable, and not an object
Was This Post Helpful? 0
  • +
  • -

#4 woooee  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 66
  • Joined: 21-November 12

Re: Accessing other object's functions outside of parent class (basic)

Posted 16 April 2013 - 12:56 PM

You didn't post how you are calling these methods, but this works for me
class Fruit(object):

    def __init__(self, typeOfFruit, number, color):
        self.theType = typeOfFruit
        self.theNumber = number
        self.theColor = color

    def getType(self):
        return self.theType

    def print_it(self):
        print self.theType, self.theNumber, self.theColor

class FruitBasket(object):

    def __init__(self):
        #the basket is empty upon initialization
        self.theBasket = []
        
    def __iter__(self):     # make iterable
        return iter(self.theBasket)
    
    def addFruit(self, currentFruit):

        duplicate = False
        new_add_type = currentFruit.getType()
        for item in self.theBasket:
           #HERE IS MY PROBLEM
           if new_add_type == item.getType():
               duplicate = True
               break

        if(duplicate == False):
            print "OK to add",
            self.theBasket.append(currentFruit)
        else:
            print "duplicate type",
        print  new_add_type
      

FB=FruitBasket()
for name, qty, color in (("Apple", 2, "Red"), ("Orange", 1, "orange"),
                         ("Apple", 1, "Yellow")):
    FruitInstance=Fruit(name, qty, color)
    FB.addFruit(FruitInstance)

print "-"*70
for instance in FB.theBasket:
    instance.print_it() 
You don't necessarily need the True/False, although there is nothing wrong with it especially if you understand it, but can use instead
    def addFruit(self, currentFruit):
        new_add_type = currentFruit.getType()
        for item in self.theBasket:
           #HERE IS MY PROBLEM
           if new_add_type == item.getType():
               print "duplicate type", new_add_type
               return

        print "OK to add", new_add_type
        self.theBasket.append(currentFruit) 

This post has been edited by woooee: 16 April 2013 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1