0
class Queue:
    def __init__(self):
        self.__part_list = []

    def put(self, elem):
        self.__part_list.append(elem)
        print(self.__part_list)

    def get(self):
        val = self.__part_list[0]
        del self.__part_list[0]
        return val

    def isempty(self):
        if len(self.__part_list) == 0:
            return True
        else:
            return False


class SuperQueue(Queue):
    def __init__(self):
        Queue.__init__(self)

    def print_list(self):
        print(self.__part_list)



que2 = Queue()
que = SuperQueue()
que.put(1)
que.put("dog")
que.put(False)
que.print_list()

AttributeError: 'SuperQueue' object has no attribute '_SuperQueue__part_list'

I can print part_list from within the superclass but not from within the subclass. I thought the subclass would inherit the list from the superclass but I cannot call it.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • 6
    The point of `__`-prefixed attribute names is to *prevent* subclasses from (easily) accessing the attribute. – chepner Jan 06 '23 at 19:50
  • According to convention AFAIK *protected* members (accessible in subclases but not outside the class) should have a single `_` prefix, not a double like *private* members. – Jared Smith Jan 06 '23 at 19:52
  • Why are you bothering to define `SuperQueue.__init__()`, when you (apparently) just want it to do the exactly what the parent `__init__()` method would do? – John Gordon Jan 06 '23 at 19:53

0 Answers0