This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author gvanrossum
Recipients Aaron Hall, brandtbucher, gvanrossum, josh.r, mark.dickinson, rhettinger, scoder, serhiy.storchaka, slam, steve.dower, xtreak
Date 2020-02-27.17:26:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1582824414.55.0.612169720779.issue36144@roundup.psfhosted.org>
In-reply-to
Content
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.)
History
Date User Action Args
2020-02-27 17:26:54gvanrossumsetrecipients: + gvanrossum, rhettinger, mark.dickinson, scoder, serhiy.storchaka, steve.dower, josh.r, Aaron Hall, slam, xtreak, brandtbucher
2020-02-27 17:26:54gvanrossumsetmessageid: <1582824414.55.0.612169720779.issue36144@roundup.psfhosted.org>
2020-02-27 17:26:54gvanrossumlinkissue36144 messages
2020-02-27 17:26:54gvanrossumcreate