[issue36144] Dictionary union. (PEP 584)
Guido van Rossum
report at bugs.python.org
Thu Feb 27 12:26:54 EST 2020
Guido van Rossum <guido at python.org> added the comment:
OK, that makes sense, it works similar to ChainMap.copy(), which copies maps[0] and keeps links to the rest. So in particular `cm | {}` will do the same thing as cm.copy().
Im not sure if the dict(other) cast is the best way to go about it. Maybe this would work?
def __or__(self, other):
new = self.copy()
new |= other # OR new.update(other) ???
return new
def __ior__(self, other):
self.update(other)
return self
Note that there is no ChainMap.update() definition -- it relies on MutableMapping.update().
I guess we need a __ror__ as well, in case there's some other mapping that doesn't implement __or__:
def __ror__(self, other):
new = other.copy()
new.update(self)
return new
Note that this doesn't return a ChainMap but an instance of type(other). If other doesn't have a copy() method it'll fail.
As a refinement, __or__ and __ror__ should perhaps check whether the operation can possibly succeed and return NotImplemented instead of raising? (Based on the type of other only, not its contents.)
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36144>
_______________________________________
More information about the Python-bugs-list
mailing list