[Python-Dev] PEP 455: TransformDict (original) (raw)

Serhiy Storchaka storchaka at gmail.com
Fri Sep 13 21:31:02 CEST 2013


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)].

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.

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.



More information about the Python-Dev mailing list