[Python-Dev] PEP 455: TransformDict

Antoine Pitrou solipsis at pitrou.net
Fri Sep 13 21:37:25 CEST 2013


On Fri, 13 Sep 2013 22:31:02 +0300
Serhiy Storchaka <storchaka at gmail.com> wrote:
> 13.09.13 21:40, Antoine Pitrou написав(ла):
> > Both are instances of a more general pattern, where a given
> > transformation function is applied to keys when looking them up: that
> > function being ``str.lower`` in the former example and the built-in
> > ``id`` function in the latter.
> 
> Please use str.casefold in examples.
> 
> >     >>> d = TransformDict(str.lower, [('Foo': 1)], Bar=2)
> 
> {'Foo': 1} or [('Foo', 1)].

Ok, thanks.

> > Providing a specialized container, not generic
> > ----------------------------------------------
> >
> > It was asked why we would provide the generic TransformDict construct
> > rather than a specialized case-insensitive dict variant.  The answer
> > is that it's nearly as cheap (code-wise and performance-wise) to provide
> > the generic construct, and it can fill more use cases.
> 
> Except lightweight IdentityDict which can be implemented more efficient 
> than TransformDict(id). It doesn't need in calling the transform 
> function, computing the hash of transformed key, comparing keys.
> But 
> perhaps in many cases TransformDict(id) is enough.

That's true. But it's only important if TransformDict is the
bottleneck. I doubt the memoizing dictionary is a bottleneck in
e.g. the pure Python implementation of pickle or json.

> > Python's own pickle module uses identity lookups for object
> > memoization:
> > http://hg.python.org/cpython/file/0e70bf1f32a3/Lib/pickle.py#l234
> 
> Also copy, json, cProfile, doctest and _threading_local.

Thanks, will add them.

Regards

Antoine.




More information about the Python-Dev mailing list