[Python-Dev] PEP 455: TransformDict
Ethan Furman
ethan at stoneleaf.us
Sat Sep 14 06:59:11 CEST 2013
On 09/13/2013 09:53 PM, Joao S. O. Bueno wrote:
> On 13 September 2013 22:40, Ethan Furman <ethan at stoneleaf.us> wrote:
>> On 09/13/2013 06:25 PM, MRAB wrote:
>>>
>>> On 14/09/2013 01:49, Steven D'Aprano wrote:
>>>>
>>>>
>>>> Is it more common to want both the canonical key and value at the same
>>>> time, or to just want the canonical key? My gut feeling is that I'm
>>>> likely to have code like this:
>>>>
>>>>
>>>> d = TransformDict(...)
>>>> for key in data:
>>>> key = d.get_canonical(key)
>>>> value = d[key]
>>>> print("{}: {}".format(key, value))
>>>>
>>> I think I must be missing something. I thought that iterating over the
>>>
>>> dict would yield the original keys, so if you wanted the original key
>>> and value you would write:
>>>
>>> for key, value in data.items():
>>> print("{}: {}".format(key, value))
>>
>>
>> Well, that's certainly how I would do it. ;)
>
> I hope you are aware that this pattern does not help when one wants
> _one_ canonical key having a non-canonical one [...]
True, but I was thinking Steve was talking about printing the entire dict, in which case that is, indeed, how I would do it.
> I mean - given no function to retrieve the canonical key,
> one would have to resort to:
>
> my_key = data.__transform__(given_key)
> for key, value in data.items():
> if data.__transform__(key) == my_key:
> ....
Which is exactly why I, and others, would like to have the transform function easily available. Besides being able to
use it to get a canonical key, one could use it to get the function itself. Yay, introspection!
--
~Ethan~
More information about the Python-Dev
mailing list