BertHobe
BertHobe

Reputation: 227

How to access an object from another class in python oop?

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?

Upvotes: 0

Views: 773

Answers (3)

user24858661
user24858661

Reputation: 1

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")

Upvotes: 0

Carlos Garzon
Carlos Garzon

Reputation: 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")```

Upvotes: -1

Mad Physicist
Mad Physicist

Reputation: 114528

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

Upvotes: 1

Related Questions