Dynamically adding builtin methods to point to a property's built-ins [duplicate]
问题 This question already has answers here : Overriding special methods on an instance (5 answers) Closed last year . I have a couple classes and a function: from functools import partial def fn(other, self, name): print(f"calling {name} with {other}") func = getattr(self.a, name) return func(other) class A: def __add__(self, other): return 9 def __mul__(self, other): return 7 def __sub__(self, other): return 8 class B: def __init__(self,a): self.a = a for name in ['add', 'sub']: name = f"__{name