0

I am currently working on the "3.2.1.16 Queue aka FIFO: part 2" lab from edube:

Your task is to slightly extend the Queue class' capabilities. We want it to have a parameterless method that returns True if the queue is empty and False otherwise.

Complete the code we've provided in the editor. Run it to check whether it outputs a similar result to ours.

Below you can copy the code we used in the previous lab.

My code so far is as follows (I have copied the code for the Queue class and my task is now to work on the SuperQueue class):

class QueueError(IndexError):
    pass


class Queue:
    def __init__(self):
        self.queue = []
    def put(self,elem):
        self.queue.insert(0,elem)
    def get(self):
        if len(self.queue) > 0:
            elem = self.queue[-1]
            del self.queue[-1]
            return elem
        else:
            raise QueueError


class SuperQueue(Queue):
    def __init__(self):
        Queue()
    def isempty(self):
        if len(Queue.self.queue)==0:
            return True
        else:
            return False
        


que = SuperQueue()
que.put(1)
que.put("dog")
que.put(False)
for i in range(4):
    if not que.isempty():
        print(que.get())
    else:
        print("Queue empty")

The crucial point is the if len(Queue.self.queue)==0. This does not work. What I want to do is to check if the length of the queue is zero. However, the queue is stored as an object in the other class. So how can I access it from the SuperQueue class?

3 Answers 3

1

The nice thing about inheritance is that a class, well, inherits: as in "owns directly", as part of itself. There's no such thing as Queue.self.queue for the child instance. The instance has a single namespace dictionary, so

if len(self.queue) == 0:

Also, get rid of the initializer in SuperQueue: if all you're doing is calling the parent __init__, you can let the parent implementation take care of it directly. If you need other functionality, call the parent method correctly:

def __init__(self):
    super().__init__()

This is python, not Java. The current implementation just calls Queue(), which creates a new instance of the parent class without initializing this one, and discards that instance immediately as the method exits.

Finally, keep in mind that virtually all python objects have a boolean value. You can rewrite the isempty method as a one liner. That makes SuperQueue super simple:

class SuperQueue(Queue):
    def isempty(self):
        return not self.queue
Sign up to request clarification or add additional context in comments.

18 Comments

Could you post a full working code? When I try to adapt it (with the if len(self.queue) == 0: solution, not return not) and I get the error message: AttributeError: 'SuperQueue' object has no attribute 'queue'
@BertHobe. Did you fix __init__?
I tried it with fixing init. I tried different variants but I get an error as I mentioned or I get syntax error. Somewhere I did a mistake, but I don't know where.
@BertHobe. I clarified what I mean about __init__. Your best option is still to just delete that method entirely and let Queue.__init__ do the work
I do not get it working. Could you maybe post a full working example for the first solution and for the other (Queue.__init__)?
|
0

Here is a full working example code:

class QueueError(IndexError):
    pass

class Queue:
    def __init__(self):
        self.queue = []
    def put(self,elem):
        self.queue.insert(0,elem)
    def get(self):
        if len(self.queue) > 0:
            elem = self.queue[-1]
            del self.queue[-1]
            return elem
        else:
            raise QueueError

class SuperQueue(Queue):
    def isempty(self):
        return len(self.queue) == 0

que = SuperQueue()
que.put(1)
que.put("dog")
que.put(False)
for i in range(4):
    if not que.isempty():
        print(que.get())
    else:
        print("Queue empty")

Comments

-1
class QueueError(IndexError):
    pass


class Queue:`enter code here`
    def __init__(self):
        self.queue = []
    def put(self,elem):
        self.queue.insert(0,elem)
    def get(self):
        if len(self.queue) > 0:
            elem = self.queue[-1]
            del self.queue[-1]
            return elem
        else:
            raise QueueError
    


class SuperQueue(Queue):
    def __init__(self):
        super().__init__()
    def isempty(self):
        return not self.queue
    


que = SuperQueue()
que.put(1)
que.put("dog")
que.put(False)
for i in range(4):
    if not que.isempty():
        print(que.get())
    else:
        print("Queue empty")```

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.