4 Replies - 1207 Views - Last Post: 28 February 2013 - 04:47 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

Passing object from other classes (very basic)

Posted 28 February 2013 - 03:03 PM

I know how to do this in Java, but it seems to have a little different syntax in Python. I have two separate classes, Fruit, and FruitBasket. I'm going to be adding a lot more functions to each classes, but they all involve passing objects around as parameters, which I'm not exactly sure how to do. This is the very basic foundation of the code.

Problem: I need to figure out how to pass a Fruit object to addFruit(fruit object) in FruitBasket. Would I pass something like fruit.Fruit? or fruit = Fruit()?

class Fruit(object):

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



class FruitBasket(object):

    def __init__(self):
        #the basket is empty upon initialization
        self.theBasket = []
        
    def addFruit(self, FRUIT OBJECT GOES HERE):
        
        #SET THE FRUIT OBJECT EQUAL TO SOMETHING?
        self.theBasket.append(FRUIT OBJECT GOES HERE)



Is This A Good Question/Topic? 0
  • +

Replies To: Passing object from other classes (very basic)

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 117
  • View blog
  • Posts: 213
  • Joined: 11-January 13

Re: Passing object from other classes (very basic)

Posted 28 February 2013 - 03:48 PM

You need to pass an instance of Fruit to an instance of FruitBasket.

MyApple = Fruit("Apple",1,"Red")
MyBasket = FruitBasket()
MyBasket.addFruit(MyApple)

-Mek
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3746
  • View blog
  • Posts: 13,116
  • Joined: 12-December 12

Re: Passing object from other classes (very basic)

Posted 28 February 2013 - 03:55 PM

You might make your class FruitBasket iterable, so that you can use code like:

for fruit in basket:
    pass

To do this, implement the __iter__ method:
class Fruit(object):

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

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):
        
        #SET THE FRUIT OBJECT EQUAL TO SOMETHING?
        self.theBasket.append(fruit)

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

secondFruit = Fruit('banana', 3, 'yellow')
basket.addFruit(secondFruit)

for afruit in basket:
    print(afruit.theType)

Otherwise, you would have to use

for fruit in basket.theBasket:

which just looks odd :whatsthat:

This post has been edited by andrewsw: 28 February 2013 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 Static Hazard  Icon User is offline

  • New D.I.C Head

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

Re: Passing object from other classes (very basic)

Posted 28 February 2013 - 04:35 PM

View PostMekire, on 28 February 2013 - 03:48 PM, said:

You need to pass an instance of Fruit to an instance of FruitBasket.

MyApple = Fruit("Apple",1,"Red")
MyBasket = FruitBasket()
MyBasket.addFruit(MyApple)

-Mek


My problem with this (let's say I'm running this code in FruitBasket.py), is Eclipse says that Fruit (on the first line) is an undefined variable, even though I want it to be an object.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3746
  • View blog
  • Posts: 13,116
  • Joined: 12-December 12

Re: Passing object from other classes (very basic)

Posted 28 February 2013 - 04:47 PM

View PostStatic Hazard, on 28 February 2013 - 04:35 PM, said:

My problem with this (let's say I'm running this code in FruitBasket.py), is Eclipse says that Fruit (on the first line) is an undefined variable, even though I want it to be an object.

Well yes, if you have these two classes in two different files, then you will need to include (import) the file that defines the Fruit class; how would it know about Fruit otherwise?!

import Fruit
# or
import Fruit from WhateverTheFileIsNamed

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1