On 09/11/2013 06:58 AM, Victor Stinner wrote:
2013/9/11 Steven D'Aprano <st...@pearwood.info>:
But the proposal is not for a case-insensitive dict. It is more general
than that, with case-insensitivity just one specific use-case for such
a transformative dict. Arguably the most natural, or at least obvious,
such transformation, but there are others.

I have code that does something like this:

MAPPING = {'spam': 23, 'ham': 42, 'eggs': 17}
result = MAPPING[key.strip()]
# later...
answer = MAPPING[key]  # Oops, forgot to strip! This is broken.

For this use case, you should not keep the key unchanged, but it's
better to store the stripped key (so MAPPING.keys() gives you the
expected result).

He isn't keeping the key unchanged (notice no white space in MAPPING), he's merely providing a function that will automatically strip the whitespace from key lookups.

The transformdict type proposed by Antoine cannot be
used for this use case.

Yes, it can.

--> from collections import transformdict
--> MAPPING = transformdict(str.strip)
--> MAPPING.update({'spam': 23, 'ham': 42, 'eggs': 17})
--> MAPPING
transformdict(<method 'strip' of 'str' objects>, {'ham': 42, 'spam': 23, 
'eggs': 17})
--> MAPPING[' eggs ']
17

--
~Ethan~
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to