Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Passing object from other classes (very basic)

$
0
0
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)


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>