1 Replies - 421 Views - Last Post: 16 April 2013 - 02:39 AM 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.

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, fruit):


firstFruit = Fruit('apple', 2, 'red')
basket = FruitBasket()

secondFruit = Fruit('banana', 3, 'yellow')

for afruit in basket:

Is This A Good Question/Topic? 0
  • +

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

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

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

Posted 16 April 2013 - 02:39 AM

So what exactly is your question?

You list a series of things you want to do and seem to suggest you know how to do them then you end with "No problem."

Is the question about how to get the 'import' statement to work as you hoped?
Or is it something else?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1