the stack is declared global in functions that change itbut not in those that just reference it the module also defines an error object (errorthat can be used to catch exceptions raised locally in this module some stack errors are built-in exceptionsthe method item triggers indexerror for out-of-bounds indexes most of the stack' functions just delegate the operation to the embedded list used to represent the stack in factthe module is really just simple wrapper around python list because this extra layer of interface logic makes clients independent of the actual implementation of the stackthoughwe're able to change the stack later without impacting its clients as usualone of the best ways to understand such code is to see it in action here' an interactive session that illustrates the module' interfaces--it implements stack of arbitrary python objectsc:\pp \dstruct\basicpython import stack stack push('spam'stack push( stack top( stack stack [ 'spam'stack pop( stack dump(stack pop('spamstack empty(true for in 'spam'stack push(cwhile not stack empty()print(stack pop()end=' stack pop(stack errorstack underflow other operations are analogousbut the main thing to notice here is that all stack operations are module functions for instanceit' possible to iterate over the stackbut we need to use counter-loops and indexing function calls (itemnothing is preventing clients from accessing (and even changingstack stack directlybut doing so defeats the purpose of interfaces like this onefor in 'spam'stack push(cstack dump(for in range(stack length())implementing stacks