2013/9/11 Steven D'Aprano <st...@pearwood.info>: > But the proposal is not for a case-insensitive dict. It is more general > than that, with case-insensitivity just one specific use-case for such > a transformative dict. Arguably the most natural, or at least obvious, > such transformation, but there are others. > > I have code that does something like this: > > MAPPING = {'spam': 23, 'ham': 42, 'eggs': 17} > result = MAPPING[key.strip()] > # later... > answer = MAPPING[key] # Oops, forgot to strip! This is broken.
For this use case, you should not keep the key unchanged, but it's better to store the stripped key (so MAPPING.keys() gives you the expected result). The transformdict type proposed by Antoine cannot be used for this use case. The os.environ mapping uses a subprocess of MutableMapping which accepts 4 functions: encoder/decoder for the key and encoder/decoder for the value. Such type is even more generic. transformdict cannot replace os._Environ. (Or do you prefer to store the ' eggs' key?) Victor _______________________________________________ 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