[Python-Dev] performance of {} versus dict()

Mark Adam dreamingforward at gmail.com
Thu Nov 15 04:24:07 CET 2012


On Wed, Nov 14, 2012 at 8:28 PM, Stephen J. Turnbull <stephen at xemacs.org> wrote:
> Chris Angelico writes:
>
>  > >>> {"a":1}+{"b":2}
>
>  > It would make sense for this to result in {"a":1,"b":2}.
>
> The test is not "does this sometimes make sense?"  It's "does this
> ever result in nonsense, and if so, do we care?"
>
> Here, addition is usually commutative.  Should {'a':1}+{'a':2} be the
> same as, or different from, {'a':2}+{'a':1}, or should it be an error?

Easy:  dict should have a (user substitutable) collision function that
is called in these cases.   This would allow significant functionality
with practically no cost.  In addition, it could be implemented in
such a way as to offer significant speedups (when using dict.update
for example) over any possible hand-written substitutes (since it's
only run on key collisions and otherwise uses an underlying loop coded
in C).

mark


More information about the Python-Dev mailing list