[Python-ideas] PEP: Dict addition and subtraction

Steven D'Aprano steve at pearwood.info
Fri Mar 15 10:59:07 EDT 2019


On Fri, Mar 15, 2019 at 12:25:22PM +0100, Antoine Pitrou wrote:

> Yeah, well.... I do think "+=" for lists was a mistake.  I *still* have
> trouble remembering the exact difference between "list +=" and
> "list.extend" (yes, there is one: one accepts more types than the
> other... which one it is, and why, I never remember;

Both accept arbitrary iterables, and the documentation suggests that 
they are the same:

https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types

Perhaps you are thinking of the difference between list + list versus 
list += iterable?


[...]
> There is a virtue to
> 
> """There should be one-- and preferably only one --obvious way to do
> it"""

"It" here refers to two different things:

"I want to update a dict in place":

The Obvious Way is to use the update method; the fact that += works as 
well is just a side-effect of the way augmented assignments are defined.

"I want a new dict that merges two existing dicts":

The Obvious Way is to use the merge operator (possibly spelled + but 
that's not written in stone yet).



-- 
Steven


More information about the Python-ideas mailing list