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.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. > defaultdict has .default_factory, so having something like .transform_key would have the added advantage of consistency.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4