dictionaries have four methods that return iteratorsdict values(for the dictionary' valuesdict items(for the dictionary' key-value itemsdict keys(for the keysand the __iter__(special method that provides support for the iter(dsyntaxand operates on the keys (actuallythe base class versions of these methods return dictionary viewsbut for most purposes the behavior of the iterators implemented here is the same since the __iter__(method and the keys(method have identical behaviorinstead of implementing keys()we simply create an object reference called keys and set it to refer to the __iter__(method with this in placeusers of sorteddict can call keys(or iter(dto get an iterator over dictionary' keysjust the same as they can call values(to get an iterator over the dictionary' values the values(and items(methods are generator methods--see the sidebar "generator functions( for brief explanation of generator methods in both cases they iterate over the sorted keys listso they always return iterators that iterate in key order (with the key order depending on the key function given to the initializerfor the items(and values(methodsthe values are looked up using the [ksyntax (which uses dict __getitem__(under the hood)since we can treat self as dict def __repr__(self)return object __repr__(selfdef __str__(self)return ("{"join(["{ ! }{ ! }format(kvfor kv in self items()]"}"we cannot provide an eval()-able representation of sorteddict because we can' produce an eval()-able representation of the key function so for the __repr__(reimplementation we bypass dict __repr__()and instead call the ultimate base class versionobject __repr__(this produces string of the kind used for non-eval()-able representationsfor example'we have implemented the sorteddict __str__(method ourselves because we want the output to show the items in key sorted order the method could have been written like this insteaditems [for keyvalue in self items()items append("{ ! }{ ! }format(keyvalue)return "{"join(items"}using list comprehension is shorter and avoids the need for the temporary items variable generators