Python 如何将3个以上的参数传递给getattr?

Python 如何将3个以上的参数传递给getattr?,python,traceback,Python,Traceback,我正在学习如何使用python的*args*和**kwargs符号。我试图使用getattr将变量量的参数传递给另一个文件中的函数 下面的代码将获取控制台输入,然后搜索包含放入控制台的函数的模块,然后使用参数执行该函数 while True: print(">>>", end = " ") consoleInput = str(input()) logging.info('Console input: {}'.format(consoleInput))

我正在学习如何使用python的
*args*
**kwargs
符号。我试图使用getattr将变量量的参数传递给另一个文件中的函数

下面的代码将获取控制台输入,然后搜索包含放入控制台的函数的模块,然后使用参数执行该函数

while True:
    print(">>>", end = " ")
    consoleInput = str(input())
    logging.info('Console input: {}'.format(consoleInput))
    commandList = consoleInput.split()
    command = commandList[0]
    print(commandList) # Debug print
    """Searches for command in imported modules, using the moduleDict dictionary,
    and the moduleCommands dictionary."""
    for key, value in moduleCommands.items():
        print(key, value)
        for commands in value:
            print(commands, value)
            if command == commands:
                args = commandList[0:]
                print(args) # Debug print
                print(getattr(moduleDict[key], command))
                func = getattr(moduleDict[key], command, *args)
                func()
                output = str(func)
    else:
        print("Command {} not found!".format(command))
        output = ("Command {} not found!".format(command))
    logging.info('Console output: {}'.format(output))
我尝试使用一个接受参数的命令,比如我所做的自定义ping命令。但是,我得到了这个回溯:

Traceback (most recent call last):
  File "/home/dorian/Desktop/DEBPSH/DEBPSH.py", line 56, in <module>
    func = getattr(moduleDict[key], command, *args)
TypeError: getattr expected at most 3 arguments, got 4
回溯(最近一次呼叫最后一次):
文件“/home/dorian/Desktop/DEBPSH/DEBPSH.py”,第56行,在
func=getattr(moduledit[key],命令,*args)
TypeError:getattr最多需要3个参数,得到4个

如何将3个以上的参数传递给
getattr
函数?

如果要将参数传递给函数,需要在调用该函数时使用这些参数
getattr()
对正在检索的属性一无所知,不接受任何调用参数

while True:
    print(">>>", end = " ")
    consoleInput = str(input())
    logging.info('Console input: {}'.format(consoleInput))
    commandList = consoleInput.split()
    command = commandList[0]
    print(commandList) # Debug print
    """Searches for command in imported modules, using the moduleDict dictionary,
    and the moduleCommands dictionary."""
    for key, value in moduleCommands.items():
        print(key, value)
        for commands in value:
            print(commands, value)
            if command == commands:
                args = commandList[0:]
                print(args) # Debug print
                print(getattr(moduleDict[key], command))
                func = getattr(moduleDict[key], command, *args)
                func()
                output = str(func)
    else:
        print("Command {} not found!".format(command))
        output = ("Command {} not found!".format(command))
    logging.info('Console output: {}'.format(output))
改为这样做:

func = getattr(moduleDict[key], command)
output = func(*args)
getattr()
参数仅获取对象、要从中检索的属性,以及属性不存在时的可选默认值


请注意,您也不需要对函数调用
str()
;充其量,您可能希望将函数调用的返回值转换为字符串。

如果要将参数传递给函数,则需要在调用该函数时使用这些参数
getattr()
对正在检索的属性一无所知,不接受任何调用参数

while True:
    print(">>>", end = " ")
    consoleInput = str(input())
    logging.info('Console input: {}'.format(consoleInput))
    commandList = consoleInput.split()
    command = commandList[0]
    print(commandList) # Debug print
    """Searches for command in imported modules, using the moduleDict dictionary,
    and the moduleCommands dictionary."""
    for key, value in moduleCommands.items():
        print(key, value)
        for commands in value:
            print(commands, value)
            if command == commands:
                args = commandList[0:]
                print(args) # Debug print
                print(getattr(moduleDict[key], command))
                func = getattr(moduleDict[key], command, *args)
                func()
                output = str(func)
    else:
        print("Command {} not found!".format(command))
        output = ("Command {} not found!".format(command))
    logging.info('Console output: {}'.format(output))
改为这样做:

func = getattr(moduleDict[key], command)
output = func(*args)
getattr()
参数仅获取对象、要从中检索的属性,以及属性不存在时的可选默认值


请注意,您也不需要对函数调用
str()
;充其量,您可能希望将函数调用的返回值转换为字符串。

回答您提出的问题;你没有。getattr只接受两个或三个参数。传递三个参数与您试图做的完全不同:

getattr(object, name[, default])
调用,比如说,
getattr(math,“sin”)
与编写
math.sin
是一样的。这将在
math
模块中检索名为
sin
的属性,该属性恰好是一个函数。使用
getattr
执行此操作没有什么特别之处。如果您编写
math.sin(i)
,您将从
math
模块获得属性
sin
,并立即调用它
getattr(math,“sin”)(i)
math.sin(i)
基本相同

由于函数调用通常是这样工作的,因此您需要以下代码:

func = getattr(moduleDict[key], command)
func(*args[1:])
这将从模块中获取命令函数,然后正常调用它,就像直接调用模块中的函数一样。不过,这并不是该部分代码中的唯一错误。接下来,你写下:

output = str(func)
这也是错误的
func
不会神奇地被调用函数的返回值替换。如果是这样的话,在你调用
math.sin
之后,没有人能再使用
math.sin
。存储在变量中的函数与任何其他函数类似,因此您显然会像任何其他函数一样检索其返回值:

func = getattr(moduleDict[key], command)
output = func(*args[1:])
除此之外,这段代码似乎还有最后一个错误<代码>命令列表[0://code>不执行任何操作。您要求从第一个元素到。。。。最后一个元素。所有这些只是复制列表。您可能首先需要的是
commandList[1:://code>,它将获得除命令之外的所有参数。解决这个问题,你会得到:

args = commandList[1:]
func = getattr(moduleDict[key], command)
func(*args)

回答你提出的问题;你没有。getattr只接受两个或三个参数。传递三个参数与您试图做的完全不同:

getattr(object, name[, default])
调用,比如说,
getattr(math,“sin”)
与编写
math.sin
是一样的。这将在
math
模块中检索名为
sin
的属性,该属性恰好是一个函数。使用
getattr
执行此操作没有什么特别之处。如果您编写
math.sin(i)
,您将从
math
模块获得属性
sin
,并立即调用它
getattr(math,“sin”)(i)
math.sin(i)
基本相同

由于函数调用通常是这样工作的,因此您需要以下代码:

func = getattr(moduleDict[key], command)
func(*args[1:])
这将从模块中获取命令函数,然后正常调用它,就像直接调用模块中的函数一样。不过,这并不是该部分代码中的唯一错误。接下来,你写下:

output = str(func)
这也是错误的
func
不会神奇地被调用函数的返回值替换。如果是这样的话,在你调用
math.sin
之后,没有人能再使用
math.sin
。存储在变量中的函数与任何其他函数类似,因此您显然会像任何其他函数一样检索其返回值:

func = getattr(moduleDict[key], command)
output = func(*args[1:])
除此之外,这段代码似乎还有最后一个错误<代码>命令列表[0://code>不执行任何操作。您要求从第一个元素到。。。。最后一个元素。所有这些只是复制列表。您可能首先需要的是
commandList[1:://code>,它将获得除命令之外的所有参数。解决这个问题,你会得到:

args = commandList[1:]
func = getattr(moduleDict[key], command)
func(*args)

也许你不应该用
*
解包元组(或其他任何东西)?它并不总是有相同数量的参数,这就是我使用
*
的原因。也许你不应该用
*
解包元组(或其他任何东西)?它并不总是有相同数量的参数,这就是我使用
*
的原因