-2

I'm a complete beginner for programming. I created the class "queue",added some elements to it and tried to print each element of the queue as following but I couldn't. Please help me! Thanks in advance!

edited for formatting

class queue:
    def __init__(self):
        self.items = []


    def isempty(self):
        return self.items == []

    def enqueue(self,item):
        self.items.insert(0,item)

    def dequeue(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

    def peek(self):
        return self.items[len(self.items)-1]

q=queue()

q.enqueue(1)
q.enqueue(2)
q.enqueue(3)

for n in q():
    print "This time, it's: "+ str(n)
Tharushi Geethma
  • 1,249
  • 15
  • 21
  • , i forgrot to enqueue elements before submission of my question! – Tharushi Geethma May 16 '15 at 04:59
  • keep in mind that a queue user should have no access to any elements of it except the next one to dequeue. – Dleep May 16 '15 at 05:23
  • @EmilianoSorbello Not necessarily. Python's own [`deque`](https://docs.python.org/2/library/collections.html#collections.deque) allows indexing as well as enqueue/dequeue operations, although the speed is O(n). – augurar May 17 '15 at 03:26

8 Answers8

2

When you create your own class implementation then you you have to define each and every behaviour of that class, the for loop is only applicable to iterables and to make your object iterable you need to define __iter__ method inside your class which would be called implicitly whenever you try to iterate over your object.

class queue:
    def __init__(self):
        self.items = []


    def isempty(self):
        return self.items == []

    def enqueue(self,item):
        self.items.insert(0,item)

    def dequeue(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

    def peek(self):
        return self.items[len(self.items)-1]

    def __iter__(self):
        for i in self.items:
            yield i

q=queue()

q.enqueue(1)
q.enqueue(2)
q.enqueue(3)

for n in q:
    print "This time, it's: "+ str(n)
ZdaR
  • 22,343
  • 7
  • 66
  • 87
1

You either need to define an __iter__ method to make your queue iterable, or you need to modify the loop to use your defined methods.

augurar
  • 12,081
  • 6
  • 50
  • 65
1

You are iterating through a non-sequence type:

for n in q():
    print "This time, it's: "+ str(n)

This is the correct way:

for n in q.items:
    print "This time, it's: "+ str(n)
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
1

Here q is an instance of class queue. Also q is not callable.And items is the instance variable.So you have to use

for n in q.items:
    print "This time, it's: "+ str(n)
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
  • This works, but it's probably better to use the object's methods than to directly access its internal state. – augurar May 17 '15 at 20:23
1

Defining the __getitem__ magic method is possibly the simplest way to make your queue iterable.

class queue:
    def __init__(self):
        self.items = []

    def isempty(self):
        return self.items == []

    def enqueue(self,item):
        self.items.insert(0, item)

    def dequeue(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

    def peek(self):
        return self.items[len(self.items)-1]

    def __getitem__(self, i):
        return self.items[i]

q=queue()

q.enqueue(1)
q.enqueue(2)
q.enqueue(3)

for n in q:
    print "This time, it's: "+ str(n)

Also don't use for n in q():. Thats going to try and call your queue object as a function. As q is not a function, it fails.

As a consequence of implementing __getitem__, you are also able to reference the elements in your queue by index directly on the queue object.

e.g.

print q[0]   

prints

3

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • I believe `__iter__` is preferable to `__getitem__` as in @ZdaR's answer (see e.g. https://stackoverflow.com/a/20551346/1349673). – James Hirschorn Feb 07 '18 at 15:30
  • 1
    @JamesHirschorn thanks for the info. It's still a worthy alternative answer in the case the user wants to access the queue elements by index., so I'll leave it in place. It's also the only answer giving this method. With the caveats mentioned in your comment, other users should be able to make an informed choice whether its best to use `__iter__` or `__getitem__`. – Paul Rooney Feb 07 '18 at 21:44
0

Well you have to enqueue something in the queue to print it out!

this code after your class

q=queue()
q.enqueue(3)
q.enqueue(4)
q.enqueue(55)

for n in q():
    print "This time, it's: "+ str(n)
Andrew Malta
  • 850
  • 5
  • 15
0

You never put anything in the queue. So it is empty. Therefore nothing gets printed. Try adding some objects (call the function q.enqueue) and then perhaps they will print.

Paul Cornelius
  • 9,245
  • 1
  • 15
  • 24
0

In my case when import Queue the answer selected above doesn't work. What really works is that:

for i in q.queue:
    print i
Jason
  • 3,166
  • 3
  • 20
  • 37