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

Serhiy Storchaka storchaka at gmail.com
Wed Sep 11 11:38:13 CEST 2013


There is a question about specifying the transform function.

There are three ways to do this:

1. Positional argument of the constructor.

d = TransformDict(str.casefold, Foo=5)

2. Subclassing.

class CaseInsensitiveDict(TransformDict):
     def transform(self, key):
         return key.casefold()
d = CaseInsensitiveDict(Foo=5)

3. Type generator.

d = TransformDict(str.casefold)(Foo=5)

Second method looks a little simpler to me from implementation side 
(What if you call __init__ for already initialized object? What if you 
used the object before calling __init__?).

Third method allows you to customize other aspects of dict behavior 
(combine OrderedDict, defaultdict,..).

First method looks less cumbersome from user side at first look. But how 
many different transform functions you use? Aren't they deserve named 
classes?




More information about the Python-Dev mailing list