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

Robert Kern robert.kern at gmail.com
Tue Jan 3 19:45:56 CET 2012

On 1/3/12 5:39 PM, Nathan Rice wrote:
> 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()?

Usually because they do not implement __eq__ using id(). The invariant that 
needs to be maintained is that if two objects compare equal, then they need to 
hash equal. Many mutable objects compare by value, and thus two non-identical 
objects can compare equal.

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

Yes, the dict.items() would be a valid, unique set, but set operations on those 
sets may not give valid dicts because the same key could point to different 
values in the two dict operands. For example:

|7> a = {'one': 1}

|8> b = {'one': '1'}

|9> set(a.items()) | set(b.items())
set([('one', '1'), ('one', 1)])

|10> dict(set(a.items()) | set(b.items()))
{'one': 1}

You've lost a value there.

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