def __iter__(self)return 'set:repr(self dataprint(self)return iter(self datafor in selfto use this classwe make instancescall methodsand run defined operators as usualfrom setwrapper import set set([ ]print( union(set([ ]))print( set([ ])prints set:[ prints set:[ overloading operations such as indexing and iteration also enables instances of our set class to often masquerade as real lists because you will interact with and extend this class in an exercise at the end of this won' say much more about this code until appendix extending types by subclassing beginning with python all the built-in types in the language can now be subclassed directly type-conversion functions such as liststrdictand tuple have become built-in type names--although transparent to your scripta type-conversion call ( list('spam')is now really an invocation of type' object constructor this change allows you to customize or extend the behavior of built-in types with userdefined class statementssimply subclass the new type names to customize them instances of your type subclasses can generally be used anywhere that the original builtin type can appear for examplesuppose you have trouble getting used to the fact that python list offsets begin at instead of not to worry--you can always code your own subclass that customizes this core behavior of lists the file typesubclass py shows howsubclass built-in list type/class map to - call back to built-in version class mylist(list)def __getitem__(selfoffset)print('(indexing % at % )(selfoffset)return list __getitem__(selfoffset if __name__ ='__main__'print(list('abc') mylist('abc'print(x__init__ inherited from list __repr__ inherited from list print( [ ]print( [ ]mylist __getitem__ customizes list superclass method append('spam')print(xx reverse()print(xattributes from list superclass in this filethe mylist subclass extends the built-in list' __getitem__ indexing method onlyto map indexes to back to the required to - all it really does is decrement extending built-in types