[Tutor] stack class

Alan Gauld alan.gauld at btinternet.com
Sat Jul 12 02:43:47 CEST 2008


"Christopher Spears" <cspears2002 at yahoo.com> wrote

> I created a stack class.  I then put the class into a script to test 
> it:

I'll assume the crazy indentation is due to email errors.

> class Stack(list):
>    def isempty(self):
>        length = len(self)
>        if length == 0:
>     return True
> else:
>     return False


return len(self) == 0

does the same thing in one line

>    def peek(self):
>        length = len(self)
> if length == 0:
>     return 0
> else:
>     last_index = length - 1
>            return self[last_index]

if len(self) > 0:
    return self[-1]
else:
    return 0

does the same thing

>    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

if len(self) > 0:
    stackpop_val = self[-1]
    self = self[:-1]
    return stackpop_val

printing is a bad idea since it will limit reusability. The default
is to return None which a class user can detect. Alternatively
raise an IndexError instead.

>    def push(self, value):
>        return self.append(value)

append just returns None so there is no need to include the
return. Just perform the append.

> 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) 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:
>     if "pop" in dir(list):
>                pop_val = stack.pop()
>         print pop_val
>     else:
>         pop_val = stack.stackpop()
> print pop_val
>        elif choice == 3:
>     push_val = raw_input("Push this value on stack: ")
>            stack.push(push_val)
>     print stack
>        elif choice == 4:
>            print "Goodbye!"
>     x = False
>        else:
>     x = False
>            sys.exit("Wrong response Goodbye!")
>
>  I am not sure how to test it because Python 2.4 is
> installed on my computer.

To fully test it you obviously need 2 versions of python
one with and one without pop. The only alternartive is to
delete pop from the built in list class on one test run.
But that might be tricky to do as pop is readonly....
ie I don't know how! :-)

The best alternative I can do is to define your own
pop to be None. That will mask the inherited method.
You can then check if Stack.pop is callable. If not call
your version.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list