(original) (raw)

On 9/10/2013 2:46 PM, Antoine Pitrou wrote:
> >> Which reminds one - this class should obviously have a method for  
> >> retrivieng the original key value, given a matching key -  
> >>  
> >> d.canonical('foo') -> 'Foo'  
> >  
> > I don't know. Is there any use case?  
> > (sure, it is trivially implemented)  
>   
>   
> Well, I'd expect it to simply be there. I had not thought of  
> other usecases for the transformdict itself -  
Well, it is not here for dict, set, etc.  

But they don't change the keys (although numbers have different representations on occasion).

One use of transformdict might be to allow use of non-hashable items as keys, by extracting an actual key from the internals of the non-hashable item. The key may be sufficiently unique to enable use of the dict structure for lookups, but it would certainly be handy to obtain the actual item again. Without a canonical lookup feature, one would be forced to also include the key as part of the value, or some such hack.

I also thought João's example was a very practical reason to have the canonical lookup feature, by some name or another.