Bob Ippolito (@etrepum) on Haskell, Python, Erlang, JavaScript, etc.

The dict.popitem conspiracy


I was creating PyProtocols interfaces for dict-like objects today, and I got stuck trying to decide whether dict.popitem is worthy of including in the interface for a mutable mapping. I have never used it, and I couldn't remember ever seeing it in use, so I checked the standard library. Other than objects that expose the same interface as dict, it seems that popitem is used once and only once in the entire standard library:

def pop(self):
    """Remove and return an arbitrary set element."""
    return self._data.popitem()[0]

I guess it might be vestige from before Python had a decent iterator protocol (though Set doesn't have the excuse of being so old).. but it's pretty ridiculous that popitem is used more often to expose dict-like interfaces than it is used to actually do something.

blog comments powered by Disqus