On Sat, Sep 14, 2013 at 02:25:42AM +0100, MRAB wrote: > On 14/09/2013 01:49, Steven D'Aprano wrote:
> >d = TransformDict(...) > >for key in data: > > key = d.get_canonical(key) > > value = d[key] > > print("{}: {}".format(key, value)) > > > I think 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)) You're missing that I'm not iterating over the entire dict, just some subset ("data") that I got from elsewhere. In real code, of course I would have to handle missing keys. My gut feeling is that normally I would want both the canonical key and the value, not just any valid key: # not this for key in data: value = d[key] print("{}: {}".format(key, value)) # but this for key in data: key, value = d.getitem(key) print("{}: {}".format(key, value)) but that's just a gut feeling, and I daresay that it would depend on the application. > and if you wanted the transformed key you would apply the transform > function to the key. The caller may not know or care what the transformation function is. The caller may only care about one or two things: - does this key match a key in the dict? - what is the canonical version of this key? For example, in a case-preserving file system, the canonical version of a file name is whatever the user first typed when they created the file: create file "SoMe FiLe" so the canonical version is, "SoMe FiLe". But any of these may be expected to match: some file SOME FILE Some File some%20file etc. It is *not* simply the case that the canonical version is "some file". Certainly that's not how case-preserving file systems work. -- Steven _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com