File: class/Workbook/Exercises/Lab5/stack2.py
stack = [] # on first import
error = 'stack2.error' # local exceptions
def push(obj): # no need for 'global'
stack.append(obj) # add item to end
def pop():
if not stack:
raise error, 'stack underflow' # raise local error
top = stack[-1]
del stack[-1] # remove last item
return top
def top():
if not stack: # raise local error
raise error, 'stack underflow' # or let IndexError
return stack[-1]
def empty(): return not stack # is the stack []?
def member(obj): return obj in stack # item in stack?
def item(offset): return stack[-(offset+1)] # index the stack
def length(): return len(stack) # number entries
def dump(): print '<Stack:%s>' % stack