2010-05-04 28 views
3

我有一些代码将一个变量从命令行传递到脚本中。我可以将var arg的任意值传递给function。问题是,当我把function放入类中时,变量不会被读入function。该脚本是:通过命令行将值传递到Python类

import sys, os 

def function(var): 
    print var 

class function_call(object): 
    def __init__(self, sysArgs): 
     try: 
      self.function = None 
      self.args = [] 
      self.modulePath = sysArgs[0] 
      self.moduleDir, tail = os.path.split(self.modulePath) 
      self.moduleName, ext = os.path.splitext(tail) 
      __import__(self.moduleName) 
      self.module = sys.modules[self.moduleName] 
      if len(sysArgs) > 1: 
       self.functionName = sysArgs[1] 
       self.function = self.module.__dict__[self.functionName] 
       self.args = sysArgs[2:] 
     except Exception, e: 
      sys.stderr.write("%s %s\n" % ("PythonCall#__init__", e)) 

    def execute(self): 
     try: 
      if self.function: 
       self.function(*self.args) 
     except Exception, e: 
      sys.stderr.write("%s %s\n" % ("PythonCall#execute", e)) 

if __name__=="__main__": 
    function_call(sys.argv).execute() 

这工作通过输入./function <function> <arg1 arg2 ....>。问题是我想选择我想要的类中的函数,而不仅仅是一个函数本身。我试过的代码是相同的,但function(var):是在一个类中。我希望得到一些关于如何修改我的function_call类来接受这个问题的想法。

如果我想通过值Hello我运行脚本像这样 - python function_call.py function Hello。然后打印var变量为Hello

通过输入变量到命令行,我可以在整个代码中使用这个变量。如果脚本是一堆函数,我可以选择使用此代码的函数,但我想选择特定类中的函数。而不是python function.py function hello我也可以进入这个类。 python function.py A function hello

另外我遇到我有问题的功能外保存使用的值。如果有人能解决这个问题,我会非常感激。 _________________________________________________________________________________

修正后的代码。这是现在为我工作的代码。

class A: 
    def __init__(self): 
     self.project = sys.argv[2] 
    def run(self, *sysArgs): 
     pass 
    def funct(self): 
     print self.project 

class function_call(object): 
    def __init__(self, sysArgs): 
     try: 
      self.function = None 
      self.args = [] 
      self.modulePath = sysArgs[0] 
      self.moduleDir, tail = os.path.split(self.modulePath) 
      self.moduleName, ext = os.path.splitext(tail) 
      __import__(self.moduleName) 
      self.module = sys.modules[self.moduleName] 
      if len(sysArgs) > 1: 
       self.functionName = sysArgs[1] 
       self.function = getattr(A(), sysArgs[1])(*sysArgs[2:]) 
       self.args = sysArgs[2:] 
     except Exception, e: 
      sys.stderr.write("%s %s\n" % ("PythonCall#__init__", e)) 

    def execute(self): 
     try: 
      if self.function: 
       self.function(*self.args) 
     except Exception, e: 
      sys.stderr.write("%s %s\n" % ("PythonCall#execute", e)) 


if __name__=="__main__": 
    function_call(sys.argv).execute() 
    inst_A = A() 
    inst_A.funct() 

感谢您的帮助。

回答

2

你会发现getattr有用:

>>> argv = ['function.py', 'run', 'Hello'] 
>>> class A: 
    def run(self, *args): 
     print(*args) 


>>> getattr(A(), argv[1])(*argv[2:]) 
Hello 
+0

@SilentGhost - 如果我只是像'def run'一样使用变量'var',我怎么能从另一个类中获得该属性?谢谢 – chrissygormley 2010-05-04 13:56:10

+0

@chriss:你在问这个问题吗?你可以在正常的调用中传递它(最后括号):'getattr(A(),'run')('arg1')' – SilentGhost 2010-05-04 14:04:21

+0

@SilentGhost - +1谢谢你,但我必须把问题留在这个答案不回答如何从该类的命令行传递参数的问题。 – chrissygormley 2010-05-04 14:12:48

1

这听起来像,而不是:

self.function = self.module.__dict__[self.functionName] 

你想要做类似的信息(如@SilentGhost提到):

self.function = getattr(some_class, self.functionName) 

在类(而不是对象实例)上检索方法的棘手问题是您是goi返回一个未绑定的方法。调用self.function时,您需要传递some_class的实例作为第一个参数。

或者,如果您正在定义有问题的类,则可以使用classmethod或staticmethod来确保some_class.function_you_want_to_pick将返回一个绑定函数。

+0

@Jeffrey Harris - +1,谢谢 – chrissygormley 2010-05-05 10:16:30

相关问题