什么是创建类的新成员函数的最佳方法,其中函数名称包含为字符串?此外,这个新函数仅作为另一个对象(帮助类)的传递,该对象具有相同的函数名称但具有可变参数。我使用 lambda 来实现这一点,但我不知道如何处理这种情况,我的传递包装器将不止一个语句(这是我的要求)
# This is a helper class
class Compensation:
   def bonus(self):
       return 10000
   def salary(self):
       # Do something
   def stack(self):
       # Do something
# This is a employer class
class employee:
   def __init__(self):
       self.compensation = Compensation()
# This is a wrapper that creates the function
def passThru(funcName):
    fn = "employee."+funcName+"=" + "lambda self, *arg: self.compensation." + funcName +"(*arg)"
    exec(fn)
fnNames = ["bonus", "salary", "stocks"]
for items in fnNames: passThru(items)
emp = employee()
emp.bonus() # returns 1000