[Python-ideas] PEP: Dict addition and subtraction
jfine2358 at gmail.com
Thu Mar 21 14:01:14 EDT 2019
Rémi Lapeyre wrote:
> Not matter the notation you end up choosing, I think this code:
> [...] 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
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.
More information about the Python-ideas