object-oriented programming def pop(selfkey*args)if key not in selfif len(args= raise keyerror(keyreturn args[ self __keys remove(keyreturn super(pop(keyargsif the given key is in the dictionary this method returns the corresponding value and removes the key-value item from the dictionary the key must also be removed from the keys list the implementation is quite subtle because the pop(method must support two different behaviors to match dict pop(the first is pop( )here the value for key is returnedor if there is no key ka keyerror is raised the second is pop(kvalue)here the value for key is returnedor if there is no key kvalue (which could be noneis returned in all casesif key existsthe corresponding item is removed def popitem(self)item super(popitem(self __keys remove(item[ ]return item the dict popitem(method removes and returns random key-value item from the dictionary we must call the base class version first since we don' know in advance which item will be removed we remove the item' key from the keys listand then return the item def clear(self)super(clear(self __keys clear(here we clear all the dictionary' items and all the keys list' items def values(self)for key in self __keysyield self[keydef items(self)for key in self __keysyield (keyself[key]def __iter__(self)return iter(self __keyskeys __iter__