-1

Below is my code

class MenuItem:

    def __init__(self, name, wholesale_cost, selling_price):
        self._name = name
        self._wholesale_cost = wholesale_cost
        self._selling_price = selling_price

    def get_menuitem_name(self):
        return self._name

class LemonadeStand:

    def __init__(self, stand_name):
        self._stand_name = stand_name
        self._current_day = 0
        self._menu_dict = {}
        self._sales_list_objects = []

    def get_name(self):
        return self._stand_name

    def add_menu_item(self, menu_item):
        # takes as a parameter a MenuItem object and adds it to
        # the menu dictionary.

        x = MenuItem.get_menuitem_name()
        self._menu_dict[x] = menu_item

        self._menu_dict[menu_item.get_menuitem_name()] = menu_item

My question is, how is it possible to have the code below?

self._menu_dict[menu_item.get_menuitem_name()] = menu_item

To me, the below code makes more sense because get_menuitem_name() is in the MenuItem class. Therefore, MenuItem.get_menuitem_name() makes sense.

x = MenuItem.get_menuitem_name()
self._menu_dict[x] = menu_item

But in the self._menu_dict[menu_item.get_menuitem_name()] = menu_item, menu_item is a parameter of the add_menu_item() method in the LemonadeStand class.

So, I'm curious to know how it is possible to call the get_menuitem_name() method from the MenuItem class in the LemonadeStand class with just a parameter from the LemonadeStand class.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Think about `MenuItem.get_menuitem_name()`. What is being passed in as the `self` argument? Nothing, so it'll fail. But if you call `menu_item.get_menuitem_name()` (where `menu_item` is an instance of `MenuItem`), the instance will be implicitly passed in as `self`, so it'll succeed. So I agree with ndc85430, you need to learn more about how classes work. – wjandrea Jul 31 '22 at 18:44
  • BTW, welcome to Stack Overflow! Check out the [tour] and [How to ask a good question](/help/how-to-ask). – wjandrea Jul 31 '22 at 18:45
  • BTW, [avoid getters and setters in Python. Use `property`s instead, or the bare attributes if possible.](/q/2627002/4518341) – wjandrea Jul 31 '22 at 18:47

1 Answers1

2

Look at the comments in the add_menu_item method - it takes "as a parameter a MenuItem object", that is an instance of the MenuItem class. From your question, it sounds like you need to do some learning about classes and how they're used. See, for example, the Python tutorial: https://docs.python.org/3/tutorial/classes.html.

ndc85430
  • 1,395
  • 3
  • 11
  • 17