def __and__(selfother)return self intersect(otherdef __or__(selfother)return self union(otherdef __repr__(self)return 'set:list __repr__(selfif __name__ ='__main__' set([ , , , ] set([ , , , , ]print(xylen( )print( intersect( ) union( )print( yx yx reverse()print(xhere is the output of the self-test code at the end of this file because subclassing core types is somewhat advanced feature with limited target audiencei'll omit further details herebut invite you to trace through these results in the code to study its behavior (which is the same on python and )python setsubclass py set:[ set:[ set:[ set:[ set:[ set:[ set:[ there are more efficient ways to implement sets with dictionaries in pythonwhich replace the nested linear search scans in the set implementations shown here with more direct dictionary index operations (hashingand so run much quicker for more detailssee the continuation of this thread in the follow-up book programming python againif you're interested in setsalso take another look at the set object type we explored in this type provides extensive set operations as built-in tools set implementations are fun to experiment withbut they are no longer strictly required in python today for another type subclassing exampleexplore the implementation of the bool type in python and later as mentioned earlier in the bookbool is subclass of int with two instances (true and falsethat behave like the integers and but inherit custom string-representation methods that display their names the "new styleclass model in release python introduced new flavor of classesknown as new-style classesclasses following the original and traditional model became known as classic classes when compared to the new kind in the class story has mergedbut it remains split for python users and codein python xall classes are automatically what were formerly called "new style,whether they explicitly inherit from object or not coding the object superclass is optional and implied the "new styleclass model