[Python-Dev] Add a "transformdict" to collections

Steven D'Aprano steve at pearwood.info
Wed Sep 11 15:40:32 CEST 2013

On Wed, Sep 11, 2013 at 06:08:25AM -0500, Skip Montanaro wrote:

> (I still don't care for the name. "Transform" != "case folding" in my
> mind. A quick scan of your links suggests most people think something
> like "cidict" or "CaseInsensitiveDict" would be more descriptive.)

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.

Using Antoine's proposal:

MAPPING = TransformDict(str.strip)
MAPPING.update({'spam': 23, 'ham': 42, 'eggs': 17})
result = MAPPING[key]
# later...
answer = MAPPING[key]  # Fine now.

so that the mapping handles stripping the keys, not the caller.

This isn't just about strings, and certainly not just 


More information about the Python-Dev mailing list