[Python-ideas] PEP: Dict addition and subtraction

Dan Sommers 2QdxY4RzWzUUiLuE at potatochowder.com
Thu Mar 21 14:13:58 EDT 2019

On 3/21/19 1:01 PM, Jonathan Fine wrote:
> Rémi Lapeyre wrote:
>> Not matter the notation you end up choosing, I think this code:
>> https://github.com/jpadilla/pyjwt/blob/master/jwt/utils.py#L71-L81
>> [...] would greatly benefit from a new merge to merge dicts.
> I've looked at the merge_dict defined in this code. It's similar to
>      def gapfill(self, other):
>          # See also: https://cobrapy.readthedocs.io/en/latest/gapfilling.html
>          # Cobra's gapfill adds items to a model, to meet a requirement.
>          for key in other.keys():
>              if key not in self:
>                  self[key] = other[key]
> (This is code I've written, that's not yet on PyPi.)  The usage is
> different. Instead of writing one of
>       aaa = merge_dict(aaa, bbb)
>       ccc = merge_dict(aaa, bbb)
> you write one of
>       gapfill(aaa, bbb)
>       aaa.gapfill(bbb)  # If gapfill added to dict methods.
> With merge_dict, you never really know if ccc is the same object as
> aaa, or a different one. Sometimes this is important.
> With gapfill, you get the same behaviour as the already familiar and
> loved dict.update. But of course with a different merge rule.

With gapfill, I can never remeber whether it's gapfill(aaa,
bbb) or gapfill(bbb, aaa).  This is always important.  :-)

At least with aaa.gapfill(bbb), I have some sense of the
"direction" of the asymmetry, or I would if I had some frame
of reference into which to put the "gapfill" operation.
(With the proposed + or | operator syntax, that gets lost.)

More information about the Python-ideas mailing list