"customize stack for usage datafrom stack import stack extends imported stack class stacklog(stack)pushes pops def __init__(selfstart=[])self maxlen stack __init__(selfstartcount pushes/popsmax-size shared/static class members could also be module vars def push(selfobject)stack push(selfobjectstacklog pushes + self maxlen max(self maxlenlen(self)do real push overall stats per-instance stats def pop(self)stacklog pops + return stack pop(selfoverall counts not 'self pops'instance def stats(self)return self maxlenself pushesself pops get counts from instance this subclass works the same as the original stackit just adds monitoring logic the new stats method is used to get statistics tuple through an instancefrom stacklog import stacklog stacklog( stacklog(make two stack objects for in range( ) push(iand push object on them for in 'spam' push(cxy run inherited __repr__ ([stack:[ ]][stack:[' '' '' '' ']] stats() stats((( )( ) pop() pop((' ' stats() stats(my maxlenall pushesall pops (( )( )notice the use of class attributes to record overall pushes and popsand instance attributes for per-instance maximum length by hanging attributes on different objectswe can expand or narrow their scopes optimizationtuple tree stacks one of the nice things about wrapping objects up in classes is that you are free to change the underlying implementation without breaking the rest of your program optimizations can be added in the futurefor instancewith minimal impactthe interface is implementing stacks