Stephanie
Stephanie

Reputation: 45

creating a class in python - error

The program I am creating should run a queue class however errors occur that append is not able to be used in the class as it does not exist, even thought it is set to a string. Could someone help me understand why I am receiving these errors?

class Queue:

    def queue(self):
        self.queue = []
        self.out_stack = []

    def enqueue(self, other='string'):
        self.enqeue = self.queue.append(other)

    def dequeue(self):
        if not self.out_stack:
            while self.queue:
                self.dequeue = self.out_stack.append(self.queue.pop(1))
        return self.dequeue

    def isEmpty(self):
        return self.queue == []

Upvotes: 0

Views: 43

Answers (1)

John La Rooy
John La Rooy

Reputation: 304473

When you create an instance variable self.queue, you are shadowing the method defined by def queue(self):

It looks like that method should perhaps be your __init__ method

class Queue:

    def __init__(self):
        self.queue = []
        self.out_stack = []

    def enqueue(self, other='string'):
        self.queue.append(other)

    def dequeue(self):               # what is this method supposed to do?
        if not self.out_stack:
            while self.queue:
                self.dequeue = self.out_stack.append(self.queue.pop(1))
        return self.dequeue

    def isEmpty(self):
        return self.queue == []

Now there is still a similar problem with self.dequeue being used as both a method and an attribute. I am not sure what you are trying to do there.

Upvotes: 2

Related Questions