python 3中使用getattr和*args时, 出现传入参数不一致的问题

今天在用python3的getattr时遇到一个问题, 就是老提示传入参数和函数前面不一致, 代码为:

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))

经过调查发现, args在Python3中类似指针, 必须用*args才能传递整个参数, 要不然是第一个参数值。

解决方案:
     def execute(self, methodname, *args):
          print(args)
          print(self.name)
          print(getattr(self.name, methodname)(*args))
posted @ 2013-07-05 09:06  muzizongheng  阅读(1198)  评论(0编辑  收藏  举报
如果我们时时忙着展现自己的知识, 将何从忆起成长所需的无知?