[Tutor] creating pop method for stack class

Christopher Spears cspears2002 at yahoo.com
Fri Jul 18 06:09:58 CEST 2008


I am almost done with a stack class that I wrote:

#!/usr/bin/python

class Stack(list):
    def isempty(self):
        length = len(self)
        if length == 0:
	    return True
	else:
	    return False
	    
    def peek(self):
        length = len(self)
	if length == 0:
	    return 0
	else:
	    last_index = length - 1
            return self[last_index]
    
    def stackpop(self):
        length = len(self)
	if length == 0:
	    print "Empty list!"
	else:
	    last_index = length - 1
	    stackpop_val = self[last_index]
	    self = self[:last_index]
	    return stackpop_val
    
    def push(self, value):
        return self.append(value)
	
if __name__ == '__main__':
    x = True
    stack = Stack()
    print "Pick an option to modify stack: "
    while x == True:
	print "1) Peek at the last value"
	print "2) Pop off the last value"
	print "3) Push a value on the stack"
	print "4) Print stack"
	print "5) Quit Program"
	choice_string = raw_input("Make a choice: ")
    
        try:
            choice = int(choice_string)
        except ValueError:
            sys.exit("Not an integer!  Goodbye!")
  
        if choice == 1:
	    if stack.isempty():
	        print "Stack is empty"
	    else:
                peek_val = stack.peek()
	        print peek_val
        elif choice == 2:
	    pop_val = stack.stackpop()
	    print pop_val
        elif choice == 3:
	    push_val = raw_input("Push this value on stack: ")
            stack.push(push_val)
        elif choice == 4:
            print stack
	elif choice == 5:
	    print "Goodbye!"
	    x = False
        else:
	    x = False
            sys.exit("Wrong response Goodbye!")

My main problem seems to be getting this part to work:

def stackpop(self):
        length = len(self)
	if length == 0:
	    print "Empty list!"
	else:
	    last_index = length - 1
	    stackpop_val = self[last_index]
	    self = self[:last_index]
	    return stackpop_val

The easiest solution would be to use the pop method from list, but I decided I wanted to take a crack at writing my own pop method.  Unfortunately, this always happens when I run the program:

1) Peek at the last value
2) Pop off the last value
3) Push a value on the stack
4) Print stack
5) Quit Program
Make a choice: 3
Push this value on stack: 1
1) Peek at the last value
2) Pop off the last value
3) Push a value on the stack
4) Print stack
5) Quit Program
Make a choice: 3
Push this value on stack: blah
1) Peek at the last value
2) Pop off the last value
3) Push a value on the stack
4) Print stack
5) Quit Program
Make a choice: 3
Push this value on stack: blah blah
1) Peek at the last value
2) Pop off the last value
3) Push a value on the stack
4) Print stack
5) Quit Program
Make a choice: 2
blah blah
1) Peek at the last value
2) Pop off the last value
3) Push a value on the stack
4) Print stack
5) Quit Program
Make a choice: 4
['1', 'blah', 'blah blah']

How come the stack doesn't shrink when I pop off the last value?  I tested the code in the interpreter:

>>> lista = [1,2,3,4]
>>> lista[:len(lista)-1]
[1, 2, 3]
>>> lista = lista[:len(lista)-1]
>>> lista
[1, 2, 3]

Any hints?


      


More information about the Tutor mailing list