Hello,
On Fri, 18 Dec 2020 06:09:56 +1100
Chris Angelico
On Fri, Dec 18, 2020 at 5:02 AM Paul Sokolovsky
wrote: This is not some completely new restriction. For example, following already doesn't work in Python:
class A: pass
o = A() o.__add__ = lambda self, x: print("me called")
o + A() # lambda above is never called
But the addition operator isn't just calling __add__, so this IS a completely new restriction. You're comparing unrelated things.
No, you're just shifting discussion to something else. Special methods assigned to object instances aren't get called in general. If you can show how to assign an arbitrary dunder method to an instance and get it called by operator, please do that. Otherwise, that was exactly the point to show.
class A: def __add__(self, other): print("I got called")
class B(A): def __add__(self, other): print("Actually I did")
A() + B()
The operator delegation mechanism doesn't use the class as a means of optimization. It does it because it is the language specification to do so.
So, the language specification for the "strict execution mode" will say that "the only way to define a method is syntactically in the class body". What's your problem with that? You often assign your methods to individual instances after they're created? Please speak up and explain us your usecases. [] -- Best regards, Paul mailto:pmiscml@gmail.com