On Tue, Dec 3, 2019 at 8:29 AM Random832 <random...@fastmail.com> wrote:
> What if there was a general mechanism to allow operators to be implemented > by user code that does not belong to the class? > > If the name [e.g.] __operatorhook_or__ is defined anywhere in a module, > within that module all calls to that operator are replaced with a two-step > process: > > call __operatorhook_or__(dict1, dict2) > if it returns a value, use that value > if it returns NotImplemented use the ordinary operator lookup process, > i.e. dict1.__or__ and dict2.__ror___ > [if __operatorhook_or__ is not defined, treat the same as if it had > returned NotImplemented] > > Then a user could simply do: > > def __operatorhook_or__(obj1, obj2): > if isinstance(obj1, dict) and isinstance(obj2, dict): > return {**obj1, **obj2} > return NotImplemented > > def __operatorhook_ior__(obj1, obj2): > if isinstance(obj1, dict): > obj1.update(obj2) > return obj1 > return NotImplemented > -1 from me. I can see someone not realizing an operator was changed, assuming it's standard semantics, and then having things break subtly. And debugging this wouldn't be fun either. To me this is monkeypatching without an explicit need for it, i.e. if you really want different semantics in your module then define a function and use that instead of influence-at-a-distance overriding of syntax.
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/YZWWDFL4LY7NEJDF5Y3GHV3URIJ6QIVQ/ Code of Conduct: http://python.org/psf/codeofconduct/