[Python-Dev] PEP 455: TransformDict

Steven D'Aprano steve at pearwood.info
Mon Oct 7 23:24:31 CEST 2013


On Fri, Oct 04, 2013 at 11:06:15PM +0200, Victor Stinner wrote:

> (If we cannot find a better name, we may add more specialized classes:
> KeyInsensitiveDict and IdentiyDict. But I like the idea of using my
> own "transform" function.)

-1 on a plethora of specialised dicts.

I do think that a TransformDict seems useful, and might even *be* 
useful, but would not like to see a whole pile of specialised dicts 
added to the std lib.

I wonder though, are we going about this the wrong way? Since there is 
apparently disagreement about TransformDict, that suggests that perhaps 
we need more concrete experience with the basic idea before graduating 
to a concrete class in the std lib. Perhaps we should follow the example 
of dict, __missing__ and defaultdict. The dict class could do something 
like this on key access:

if type(self) is not dict:
    # This only applies to subclasses, not dict itself.
    try:
        transform = type(self).__transform__
    except AttributeError:
        pass
    else:
        key = transform(key)
# now use the key as usual


Am I barking up the wrong tree? Would this slow down dict access too 
much?


-- 
Steven


More information about the Python-Dev mailing list