3

I'm working through the MTIx 6.00.1x Intro to Computer Science class, and am having trouble creating class methods. Specifically, the 'remove' function within my 'Queue' class does not return the value as I'd expect.

Here's context on the request:

For this exercise, you will be coding your very first class, a Queue class. In your Queue class, you will need three methods:

init: initialize your Queue (think: how will you store the queue's elements? You'll need to initialize an appropriate object attribute in this method)

insert: inserts one element in your Queue

remove: removes (or 'pops') one element from your Queue and returns it. If the queue is empty, raises a ValueError.

I've written the following code with a 'remove' method, but though the method's behavior correctly alters the array, it doesn't return the 'popped' value:

class Queue(object):

    def __init__(self):
        self.vals = []

    def insert(self, value):
        self.vals.append(value)

    def remove(self):
        try:
            self.vals.pop(0)
        except:
            raise ValueError()

Any help would be greatly appreciated!

joanx737
  • 337
  • 1
  • 3
  • 19
  • To avoid future confusion: a “class method” in Python usually means [something different](http://stackoverflow.com/q/136097/1392132). Your functions are just ordinary *function attributes*. – 5gon12eder Mar 01 '15 at 20:08

3 Answers3

2

Well, returning is rather easy in Python, so just do this:

def remove(self):
    try:
        return self.vals.pop(0)
    except:
        raise ValueError()

Luckily, pop() already removes and returns the selected element at the same time.

TidB
  • 1,749
  • 1
  • 12
  • 14
  • Thanks for the response! My confusion is this: say 'c' is a 'Queue' object. wouldn't c.remove() be equivalent to calling c.vals.pop(0), which would be expected to return the removed value automatically? – joanx737 Mar 01 '15 at 20:12
  • No, `remove()` doesn't remove by index, but by the value. – TidB Mar 01 '15 at 20:20
  • Sorry, I was specifically referring to the 'queue' class's custom 'remove' method (which utilizes pop() ) vs. Python's built-in method; if it calls pop(), why isn't the value automatically returned? Thanks! – joanx737 Mar 02 '15 at 01:29
0

You need to use return for returning values. Update your remove method to:

def remove(self):
     try:
         return self.vals.pop(0)
     except:
         raise ValueError
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

You have to explicitly return that value:

return self.vals.pop()

Note also that:

  • the argument to list.pop() method is optional;
  • it also will raise an IndexError, so you should catch only that specific exception instead of every exception;
  • you should use exception chaining if possible;
  • your vals member is kind of private, so rename it to start with an underscore;
  • the whole task is somewhat pointless, as Python lists already have append() and pop() methods with exactly required behaviour.
Community
  • 1
  • 1
firegurafiku
  • 3,017
  • 1
  • 28
  • 37