Le Tue, 10 Sep 2013 07:18:41 -0700, Eli Bendersky email@example.com a écrit :
On Tue, Sep 10, 2013 at 5:22 AM, Antoine Pitrou firstname.lastname@example.org wrote:
Le Tue, 10 Sep 2013 22:00:37 +1000, Nick Coghlan email@example.com a écrit :
Is this just syntactic sugar for recursive lookup of a transformed version in __missing__?
Nope. For one, it doesn't use __missing__ at all. I think using __missing__ would be... missing the point, because it wouldn't working properly if you have e.g. X != Y such that transform(X) == Y and transform(Y) == X.
(a simple example being transform = str.swapcase :-))
Or a way of supplying a custom "key" function to a dictionary?
Probably, although I'm not entirely sure what you mean by that :-)
Any such proposal should consider how it composes with other dict variants like defaultdict, OrderedDict and counter.
Well, one sticking point here is that those variants don't compose with each other already :-) But, yes, I may make another proposal with composition in mind.
Does anyone have an idea how to make the existing variants more composable? It would be nice to get a better understanding of this before we add another variant. Conceptually, composability makes a lot of sense (what if we want this transformdict but also in insertion order...)
AFAICT, the main problem with composability is that the implementation is less simple and it removes many opportunities for optimization. It may or may not be a problem, depending on your use cases.
(I'm playing a bit with a TransformMap which is a composable version of transformdict, and it's clear the implementation is less amenable to optimization tweaks and shortcuts: in fact, I have to *add* some logic to e.g. cater with defaultdict)