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

Devin Jeanpierre jeanpierreda at gmail.com
Tue Jan 3 21:50:51 CET 2012


> 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).

He only describes <, which can be implemented in linear time as:

    def __lt__(self, d2):
        if not isinstance(d2, dict):
            return NotImplemented

        return all(key in d2 and d2[key] == value for key, value in
self.items())

Which set operations are you thinking of?

-- Devin

On Tue, Jan 3, 2012 at 12:10 PM, Robert Kern <robert.kern at gmail.com> wrote:
> 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
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas



More information about the Python-ideas mailing list