[Python-ideas] [Python-Dev] hello, new dict addition for new eve ?
nathan.alexander.rice at gmail.com
Tue Jan 3 18:39:48 CET 2012
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).
I haven't had any problems with using set(somedict.items()). I will
admit that I primarily do this in simple contexts.
This brings me to another curiosity... Why do mutable items not
implement hash using id()?
> Further, you cannot treat dicts as sets of (key, value) pairs because dicts
> have unique keys, not unique (key, value) pairs.
I'm confused. Because keys are unique, (key, value) pairs are unique
as well. I realize the values are not unique but the tuple is
guaranteed to be.
>> 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.
Recommendation definitely noted :D
More information about the Python-ideas