res [for in selfif in otherres append(xreturn set(resother is any sequence type self is the instance subject def union(selfother)res set(selfres concat(otherreturn res return new set def concat(selfvalue)for in valueif not in selfself append(xnew set with copy of my list insert uniques from other valuea liststringset filters out duplicates lengetitemiter inheriteduse list repr def __and__(selfother)return self intersect(otherdef __or__(selfother)return self union(otherdef __str__(self)return 'class fastset(dict)pass this doesn' simplify much self-test code omittedsee examples package file the stack and set implemented in this code are essentially like those we saw earlierbut instead of embedding and managing listthese objects really are customized lists they add few additional methodsbut they inherit all of the list object' functionality this can reduce the amount of wrapper code requiredbut it can also expose functionality that might not be appropriate in some cases as codedfor examplewe're able to sort and insert into stacks and reverse setbecause we've inherited these methods from the built-in list object in most casessuch operations don' make sense for these data structuresand barring extra code that disables such nonfeaturesthe wrapper class approach of the prior sections may still be preferred for more on the class subtype classessee the remainder of their implementation file in the examples package for self-test code and its expected output because these objects are used in the same way as our original stacks and setsinteracting with them is left as suggested exercise here subclassing built-in types has other applicationswhich may be more useful than those demonstrated by the preceding code consider queueor ordered dictionaryfor example the queue could take the form of list subclass with get and put methods to insert on one end and delete from the otherthe dictionary could be coded as dictionary subclass with an extra list of keys that is sorted on insertion or request while this scheme works well for types that resemble built-insthoughtype subclasses may data structures