1,直接调用
class ClassA:
def method(self):
print("HELLO WORLD")
obj = ClassA()
obj.method() # 直接调用对象的方法
2,getattr函数
class ClassB:
def method(self,param):
print(f"{param} NO BUG")
obj = ClassB()
method = getattr(obj, 'method')
method('me') # 调用获取到的方法
3,operator.methodcaller
import operator
class ClassC:
def method(self, param):
print(f"Method called with argument: {param}")
obj = ClassC()
caller = operator.methodcaller('method', 'Hello')
caller(obj) # 调用对象的方法并传递参数