[Python-Dev] PEP 455: TransformDict

R. David Murray rdmurray at bitdance.com
Fri Sep 13 22:09:10 CEST 2013


On Fri, 13 Sep 2013 20:40:58 +0200, Antoine Pitrou <solipsis at pitrou.net> wrote:
> Rationale
> =========
> 
> Numerous specialized versions of this pattern exist.  The most common
> is a case-insensitive case-preserving dict, i.e. a dict-like container
> which matches keys in a case-insensitive fashion but retains the
> original casing.  It is a very common need in network programming, as
> many protocols feature some arrays of "key / value" properties in their
> messages, where the keys are textual strings whose casing isn't
> relevant.

This motivation would be stronger if the last phrase was something like
"where the keys are textual strings whose case is specified to be ignored
on receipt but by either specification or custom is to be preserved
or non-trivially canonicalized when retransmitted."

> (it can be said that the pattern *projects* keys from the user-visible
> set onto the internal lookup set, hence this PEP's title)

Not clear what "projects" has to do with the PEP title.

> Constructor
> -----------
> 
> As shown in the example aboves, creating a TransformDict requires
> passing the key transformation function as the first argument (much
> like creating a ``defaultdict`` requires passing the factory function
> as first argument).
> 
> The constructor also takes other optional arguments which can be used
> to initialize the TransformDict with certain key-value pairs.  Those
> optional arguments are the same as in the ``dict`` and ``defaultdict``
> constructors::
> 
>    >>> d = TransformDict(str.lower, [('Foo': 1)], Bar=2)
>    >>> sorted(d.items())
>    [('Bar', 2), ('Foo', 1)]
> 
> 
> Alternative proposals and questions
> ===================================

You don't mention the alternate constructor discussion, or the
rationale for following the defaultdict pattern (ie: following the
established pattern :)

--David


More information about the Python-Dev mailing list