I am following this tutorial on Python Institute.
I have three classes as shown below. Parent class Queue has put and get functions. get function raises QueueError if queue is empty. The child class SuperQueue has to add isempty function which returns true if queue is empty. My first approach was to call parent class get function inside try block and return its returned value if present otherwise catch error in except block and return true.My second approach shown in code was to create get function for child class. In that function I grab returnd value in a variable. If returned value is not emppty I put it back into queue and return it to the caller. In this way when I call get function inside isempty function the queue is intact.The problem here is that the driver program after entering some items into queue it calls isempty function and if queue is not empty it calls get function to print items. So get function is called twice and my code does not print entered items in the order they were entered.I need help how to emplement isempty function.
class QueueError: passclass 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.__init__(self) def get(self): try: v= Queue.get(self) return v except: print('exception') def isempty(self): v=self.get() if v: self.put(v) return False return True 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") #cannot get queue items in order . I get the second Item first and cannot get the third (False)