python 3中使用getattr和*args时, 出现传入参数不一致的问题
class Test:
def __init__(self, name):
self.name = name
def execute(self, methodname, *args):
print(args)
print(self.name)
print(getattr(self.name, methodname)(args))
def __init__(self, name):
self.name = name
def execute(self, methodname, *args):
print(args)
print(self.name)
print(getattr(self.name, methodname)(args))
经过调查发现, args在Python3中类似指针, 必须用*args才能传递整个参数, 要不然是第一个参数值。
解决方案:
def execute(self, methodname, *args):
print(args)
print(self.name)
print(getattr(self.name, methodname)(*args))
print(args)
print(self.name)
print(getattr(self.name, methodname)(*args))
https://muzizongheng.blog.csdn.net/