[Python-Dev] PEP 455: TransformDict

Ethan Furman ethan at stoneleaf.us
Sat Sep 14 03:40:06 CEST 2013


On 09/13/2013 06:25 PM, MRAB wrote:
> On 14/09/2013 01:49, Steven D'Aprano wrote:
>>
>> Is it more common to want both the canonical key and value at the same
>> time, or to just want the canonical key? My gut feeling is that I'm
>> likely to have code like this:
>>
>>
>> d = TransformDict(...)
>> for key in data:
>>      key = d.get_canonical(key)
>>      value = d[key]
>>      print("{}: {}".format(key, value))
>>
> I think I must be missing something. I thought that iterating over the
> dict would yield the original keys, so if you wanted the original key
> and value you would write:
>
> for key, value in data.items():
>      print("{}: {}".format(key, value))

Well, that's certainly how I would do it.  ;)


> and if you wanted the transformed key you would apply the transform
> function to the key.

Indeed.  The question is:  how?  It is entirely possible that your function has a TransformDict alone, and no memory of 
the transform function used to create the dict...

If the key transform function were saved directly on the TransformDict instance as, say, .transform_key, then problem 
solved.

--
~Ethan~


More information about the Python-Dev mailing list