[Python-ideas] Adding "+" and "+=" operators to dict
Alexander Heger
python at 2sn.net
Fri Feb 13 22:11:01 CET 2015
> Some languages may be able to optimize a + b + c + d to avoid making and
> throwing away the intermediate dicts, but in general Python cannot. So
> it is going to fall into the same trap as list addition does. While it
> is not common for this to lead to severe performance degradation, when
> it does happen the cost is *so severe* that we should think long and
> hard before adding any more O(N**2) booby traps into the language.
you can still have both, make the + operator work for simple cases and
warn people to use the function interface for more time-critical
complicated cases.
And sometimes you may want have certain behavior, e.g., a + ((b + c) +
d), especially if you did overwrite __add__ for a custom behaviour
(using default update on standard dicts this would be the same result,
I think).
-Alexander
More information about the Python-ideas
mailing list