Message362821
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.) |
|
Date |
User |
Action |
Args |
2020-02-27 17:26:54 | gvanrossum | set | recipients:
+ gvanrossum, rhettinger, mark.dickinson, scoder, serhiy.storchaka, steve.dower, josh.r, Aaron Hall, slam, xtreak, brandtbucher |
2020-02-27 17:26:54 | gvanrossum | set | messageid: <1582824414.55.0.612169720779.issue36144@roundup.psfhosted.org> |
2020-02-27 17:26:54 | gvanrossum | link | issue36144 messages |
2020-02-27 17:26:54 | gvanrossum | create | |
|