
On 2/6/20 4:23 PM, Serhiy Storchaka wrote:
It would create an exception of two rules:
1. Operators on subclasses of builtin classes do not depend on overridden methods of arguments (except the corresponding dunder method). `list.__add__` and `set.__or__` do not call copy() and extend()/update(). You should override the corresponding dunder method to change the behavior of the operator.
2. Operators do not depend on non-dunder methods.
This looks to me as a direct violation of the principle "Special cases aren't special enough to break the rules."
I may not fully understand the implications of #1, but I would think you could implement the semantics Brandt wants using only dunder methods and copy.copy (which itself dispatches to one of a number of dunder methods - __copy__, __reduce__, __setstate__, depending on which ones are defined - we could presumably avoid the `copy` import by partially porting that logic into `__or__`): def __or__(self, other): new_value = copy.copy(self) for key in other.__keys__(): new_value.__setitem__(key, other.__getitem__(key)) return new_value Obviously the actual implementation would be in C and handle more edge cases and whatnot, but I think the desired semantics can all be achieved using only magic methods on the objects themselves (though we'd probably want to bypass all that stuff in favor of a "fast path" in the case of `dict` | `dict`). Best, Paul