incurs some performance degradation for the extra method callsbut it supports future changes better by encapsulating stack operations example - pp \dstruct\basic\stack py "optimize with in-place list operationsclass error(exception)pass when importedlocal exception class stackdef __init__(selfstart=[])self stack [for in startself push(xself is the instance object start is any sequencestack def push(selfobj)self stack append(objmethodslike module self top is end of list def pop(self)if not self stackraise error('underflow'return self stack pop(like fetch and delete stack[- def top(self)if not self stackraise error('underflow'return self stack[- def empty(self)return not self stack instance empty(def __len__(self)return len(self stacklen(instance)not instance def __getitem__(selfoffset)return self stack[offsetinstance[offset]infor def __repr__(self)return '[stack:% ]self stack this version works like the original in module stack toojust replace stack with stack in the previous interaction to get feel for its operation the only obvious difference is that stack items are in reverse when printed ( the top is the end)from stack import stack stack( push('spam' push( [stack:['spam' ] stack( push( push( pop()xy data structures