[Python-Dev] Add a "transformdict" to collections

Ethan Furman ethan at stoneleaf.us
Tue Sep 10 16:59:22 CEST 2013


On 09/10/2013 07:54 AM, Antoine Pitrou wrote:
> Le Tue, 10 Sep 2013 15:09:56 +0200,
> Hrvoje Niksic <hrvoje.niksic at avl.com> a écrit :
>> On 09/10/2013 02:24 PM, Paul Moore wrote:
>>>>>> td['FOO'] = 42
>>>>>> td['foo'] = 32
>>>>>> list(td.keys())
>>>
>>> ['FOO'] or ['foo']? Both answers are justifiable.
>>
>> Note that the same question can be reasonably asked for dict itself:
>>
>>   >>> d = {}
>>   >>> d[1.0] = 'foo'
>>   >>> d[1] = 'bar'
>>   >>> d
>> {1.0: 'bar'}
>>
>> So, dict.__setitem__ only replaces the value, leaving the original
>> key in place. transformdict should probably do the same, returning
>> 'FOO' in your example.
>
> It's not that obvious. It's not common to rely on that aspect of dict
> semantics, because you will usually lookup using the exact same type,
> not a compatible one. I would expect very little code, if any, to rely
> on this.
>
> (also, I would intuitively expect the latest key to be held, not the
> first one, since that's what happens for values.)

Whether there is a bunch of code that relies on this behavior is irrelevant.  That behavior is already in place, and 
having another dict that is just the opposite will only lead to more confusion, not less.

--
~Ethan~


More information about the Python-Dev mailing list