Python classes and the __init___ constructor, how to make a list variable?

  class, constructor, getter-setter, list, python

Hi I am creating a class that has a shopping list. The code seems to work but I’m not sure if I’m doing it correctly. Am I meant to put shopping = [] in the constructor, what does that do?
Many thanks in advance

class Shopping:
    def __init__(self, shopping = []): 
        self.__shopping = []

   
    def set_shopping(self, shopping): 
          self.__shopping  = shopping
    def get_shopping(self): 
        return self.__shopping

shoppinglist = Shopping()
shoppinglist = ['apples', 'pears', 'bread']

Source: Python Questions

LEAVE A COMMENT