[Python-ideas] Why operators are useful

Steven D'Aprano steve at pearwood.info
Sat Mar 16 06:33:31 EDT 2019


On Fri, Mar 15, 2019 at 10:53:31PM +0000, MRAB wrote:

> There was also the suggestion of having both << and >>.
> 
> Actually, now that dicts are ordered, that would provide a use-case, 
> because you would then be able to choose which values were overwritten 
> whilst maintaining the order of the dict on the LHS.

Is that common enough that it needs to be built-in to dict itself?

If it is uncommon, then the conventional solution is to subclass dict, 
overriding the merge operator to use first-seen semantics.

The question this PEP is trying to answer is not "can we support every 
use-case imaginable for a merge operator?" but "can we support the most 
typical use-case?", which I believe is a version of:

    new = a.copy()
    new.update(b)
    # do something with new


-- 
Steven


More information about the Python-ideas mailing list