[Python-Dev] PEP 455: TransformDict (original) (raw)

MRAB python at mrabarnett.plus.com
Sat Sep 14 04:39:18 CEST 2013


On 14/09/2013 02:40, Ethan Furman wrote:

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.getcanonical(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, .transformkey, then problem solved. defaultdict has .default_factory, so having something like .transform_key would have the added advantage of consistency.



More information about the Python-Dev mailing list