Let's say we have two classes Basket and Item. Each basket contains a list of items. I can store in the basket the ids of the items, or directly the items objects. Which is the best solution, and why (in terms of memory usage, covenience, etc...)?
CASE A
class Basket(object):
def __init__(self, basket_name):
self._name = basket_name
self._items = []
def add_item(self, item_id)
self._items.append(item_id)
class Item(object):
def __init__(self, item_id):
self._id= item_id
>>> b = Basket('My_Basket')
>>> i1 = Item(1)
>>> i2 = Item(2)
>>> b.add_item(1)
>>> b.add_item(2)
CASE B
class Basket(object):
def __init__(self, basket_name):
self._name = basket_name
self._items = []
def add_item(self, item)
self._items.append(item)
class Item(object):
def __init__(self, item_id):
self._id= item_id
>>> b = Basket('My_Basket')
>>> i1 = Item(1)
>>> i2 = Item(2)
>>> b.add_item(i1)
>>> b.add_item(i2)
Itemin aBasketis more "object oriented" than adding another level of indirection.Iteminstances havingBasketinstance in snippet A, but it's possible in snippet B.