[Python-ideas] [Python-Dev] hello, new dict addition for new eve ?

Robert Kern robert.kern at gmail.com
Tue Jan 3 18:10:51 CET 2012


On 1/3/12 4:59 PM, Nathan Rice wrote:
> This is slightly tangential, but I've always wondered... Why aren't
> set operations implemented on dicts?  It is fairly natural to view a
> dictionary as a set of (key, value) pairs.  Things like
> subset/superset checking (with concomitant operator support) make
> sense.  I have written stuff like set(dict1.items())<
> set(dict2.items()) many times.

The values are unrestricted Python objects. They do not have to be hashable or 
sortable. The set operations you describe would have to be require one or both 
(or else do something algorithmically horrendous).

Further, you cannot treat dicts as sets of (key, value) pairs because dicts have 
unique keys, not unique (key, value) pairs.

> I don't even know what rich comparison operators on dictionaries do
> now, it isn't intuitive at all.

The rich comparison operators are only defined for == and !=. In Python 2.x, 
there is a legacy implementation of __cmp__ that does something more 
complicated. I recommend ignoring it.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-ideas mailing list