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

"Martin v. Löwis" martin at v.loewis.de
Wed Sep 11 19:31:56 CEST 2013

Am 11.09.13 15:04, schrieb Antoine Pitrou:
> There are not many possible APIs to create case-insensitive dicts, or
> identity dicts. 

That is certainly not true. Most obviously, you have the choice of a
specialized case-mapping dict, or a generalized type that can be used
for case mapping also. Does any of the referenced use cases actually
use the API that you are proposing (i.e. with a key transformation

FWIW, I can think of yet another API for this: write a Proxy class

class TransformedKey: # untested
  def __init__(self, original):
    self.original = original
    self.transformed = self.transform(original)
  def __hash__(self):
    return hash(self.transformed)
  def __eq__(self, other):
    return self.transformed == other.transformed
  def transform(self, key):
     raise NotImplementedError

If there is then disciplined use in the form

  d[TransformedKey(key)] == value

then you can use the existing dictionary type. Notice that this can do
both case-insensitive and identity dicts (plus there are multiple
choices of getting the transform function into it, as well as
variations of the __eq__ implementation).

There is evidence in this thread that people "grasp" case-insensitive
more easily than the generalized API. For the record, I initially typed
two responses into the tracker which I found to be incorrect before
posting them, so I ended up posting neither. The "transformdict" type
is not at all "natural", even though it may be useful.

If you really want to "push" this API into 3.4, I think you will need
to write a PEP, and find a PEP dictator who is willing to approve it.
As you seem to dislike the idea of writing a PEP, I suggest to follow
the idea of publishing it on PyPI now, and then proposing it for
inclusion into 3.5.


More information about the Python-Dev mailing list