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