we keep copy of all the dictionary' keys in sorted list stored in the self __keys variable we pass the dictionary' keys to initialize the sorted list using the base class' dict keys(method--we must not use sorteddict keys(because that relies on the self __keys variable which will exist only after the sortedlist of keys has been created def update(selfdictionary=none**kwargs)if dictionary is nonepass elif isinstance(dictionarydict)super(update(dictionaryelsefor keyvalue in dictionary items()super(__setitem__(keyvalueif kwargssuper(update(kwargsself __keys sortedlist sortedlist(super(keys()self __keys keythis method is used to update one dictionary' items with another dictionary' itemsor with keyword argumentsor both items which exist only in the other dictionary are added to this oneand for items whose keys appear in both dictionariesthe other dictionary' value replaces the original value we have had to extend the behavior slightly in that we keep the original dictionary' key functioneven if the other dictionary is sorteddict the updating is done in two phases first we update the dictionary' items if the given dictionary is dict subclass (which includes sorteddictof course)we use the base class dict update(to perform the update--using the base class version is essential to avoid calling sorteddict update(recursively and going into an infinite loop if the dictionary is not dict we iterate over the dictionary' items and set each key-value pair individually (if the dictionary object is not dict and does not have an items(method an attributeerror exception will quite rightly be raised if keyword arguments have been used we again call the base class update(method to incorporate them consequence of the updating is that the self __keys list becomes out of dateso we replace it with new sortedlist with the dictionary' keys (again obtained from the base classsince the sorteddict keys(method relies on the self __keys list which we are in the process of updating)and with the original sorted list' key function @classmethod def fromkeys(clsiterablevalue=nonekey=none)return cls({kvalue for in iterable}key