object-oriented programming potentially more efficient approach an image stores single background colorplus the colors of those points in the image that differ from the background color this is done by using dictionary as kind of sparse arraywith each key being an (xycoordinate and the corresponding value being the color of that point if we had image and half its points are the background colorwe would need to store only colorsa considerable saving in memory the image py module follows what should now be familiar patternit starts with shebang linethen copyright information in commentsthen module docstring with some doctestsand then the importsin this case of the os and pickle modules we will briefly cover the use of the pickle module when we cover saving and loading images after the imports we create some custom exception classesclass imageerror(exception)pass class coordinateerror(imageerror)pass we have shown only the first two exception classesthe others (loaderrorsaveerrorexporterrorand nofilenameerrorare all created the same way and all inherit from imageerror users of the image class can choose to test for any of the specific exceptionsor just for the base class imageerror exception the rest of the module consists of the image class and at the end the standard three lines for running the module' doctests before looking at the class and its methodslet' look at how it can be usedborder_color "#ff red square_color "# ffblue widthheight midxmidy width / height / image image image(widthheight"square_eye img"for in range(width)for in range(height)if width or height image[xyborder_color elif midx midx and midy midy image[xysquare_color image save(image export("square_eye xpm"notice that we can use the item access operator ([]for setting colors in the image brackets can also be used for getting or deleting (effectively setting to the background colorthe color at particular (xycoordinate the coordinates are passed as single tuple object (thanks to the comma operator)the same as if we wrote image[(xy)achieving this kind of seamless syntax integration is easy in python--we just have to implement the appropriate special methodspickles