[Python-Dev] Add a "transformdict" to collections
storchaka at gmail.com
Wed Sep 11 12:37:53 CEST 2013
11.09.13 12:52, Antoine Pitrou написав(ла):
> Le Wed, 11 Sep 2013 12:38:13 +0300,
> Serhiy Storchaka <storchaka at gmail.com> a écrit :
>> 2. Subclassing.
>> class CaseInsensitiveDict(TransformDict):
>> def transform(self, key):
>> return key.casefold()
>> d = CaseInsensitiveDict(Foo=5)
> I thought about this first, and then I remembered that python-dev isn't
> generally very keen on subclassing-based APIs :-)
Why? This way looks most natural to me.
>> 3. Type generator.
>> d = TransformDict(str.casefold)(Foo=5)
>> Third method allows you to customize other aspects of dict behavior
>> (combine OrderedDict, defaultdict,..).
> Well, no, it's not that easy. Especially since OrderedDict and
> defaultdict weren't written with combination in mind.
They can be rewritten.
Actually the defaultdict is just simple wrapper around existing
functionality of the __missing__ method. We can add the __transform__
method directly in the dict class. I think it will significant (2-3x)
decrease a size of needed C code (no need in defining new type with
constructors/destructors/etc, only lookup_transform).
More information about the Python-Dev