简体   繁体   English

python:如何仅使用字符串名称将变量传递给函数

[英]python: how can I pass a variable to a function with only the string name

I'm interacting with a relatively complex program and putting a GUI on it to issue commands. 我正在与一个相对复杂的程序进行交互,并在其上放置一个GUI来发出命令。 I made up a simple example which I think demonstrates the problem. 我做了一个简单的例子,我认为可以说明问题。 I know what parameters need to be passed to issue a command but I will only be able to get them as strings. 我知道发出命令需要传递哪些参数,但是我只能将它们作为字符串获取。

There are hundreds of commands with different return values that I get dynamically, but this is one example what I can get back when I pass a command ID 我动态获得了数百个具有不同返回值的命令,但这是一个示例,当我传递命令ID时可以取回

def get_command_vars(commandID = None):
    if commandID == '0x4F':
    return [['mode', '16bits'], ['seconds', '8bits']]


def issueMyCommand(commandID = None):
    commandParameters = get_command_vars(command=0x4F)

commandParameters tells me that the parameters for this command are mode and seconds, but they are strings commandParameters告诉我此命令的参数是mode和seconds,但它们是字符串

commandParm_1 = commandParameters[0][0] # 'mode'
commandParm_2 = commandParameters[1][0] # 'seconds'

>get User Input from the gui to pass to issuetheCommand
input1 = getinputEntry1() # this is the 'mode' value, e.g., 8
input2 = getinputEntry2() # this is the 'seconds' value, e.g., 14

I have the values to pass from the user input but I don't know how to pass them to the function since I only have the variables as strings, ie'mode' and 'seconds' 我有要从用户输入传递的值,但是我不知道如何将它们传递给函数,因为我只有变量作为字符串,即“ mode”和“ seconds”

c = issueTheCommand(mode = input1, seconds = input2)

this command format will change based on the parameter types from get_command_vars, so it could be 'count', 'datalength', 'milliseconds, 'delay', etc 该命令格式将根据get_command_vars中的参数类型而更改,因此它可能是'count','datalength','milliseconds,'delay'等

@sberry - actually the user input values are what will be passed with mode and seconds. @sberry-实际上,用户输入值是将通过mode和seconds传递的值。 the 16bits and 8 bits doesn't really come into play here. 16位和8位在这里并没有真正发挥作用。 I'm trying to do this without changing the format that "issueTheCommand" function is expecting if possible. 我试图做到这一点,如果可能的话,不更改“ issueTheCommand”函数期望的格式。 the way I issue it now looks like this: c = issueTheCommand(mode = 8, seconds = 14). 我发出它的方式现在看起来像这样:c = issueTheCommand(mode = 8,seconds = 14)。 i don't think it will take a dict? 我不认为这会成文吗?

If my comment to your question is what you are after then perhaps using a dictionary as key-word arguments would work for you. 如果我对您的问题的评论是您要做什么,那么也许使用字典作为关键字参数对您有用。

>>> def issueTheCommand(mode, seconds):
...     print mode, seconds
...
>>> issueTheCommand(**{"mode": "16bits", "seconds": "8bits"})
16bits 8bits

Arguments specified as keywords can be picked up as a dict. 指定为关键字的参数可以作为字典使用。 Conversely, keyword arguments can be provided as a dict. 相反,关键字参数可以作为字典提供。 Look: 看:

>>> def a(**b):   # Pick up keyword args in a dict named b.
...   print(b)
... 
>>> a(x=1, y=2)
{'y': 2, 'x': 1}
>>> c = {'y': 2, 'x': 1}
>>> a(**c)
{'y': 2, 'x': 1}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python (Kivy) 中将变量从函数传递到函数 - How can I pass a variable from function to function in Python (Kivy) 如何保留原始字符串(作为变量)以便我可以将其传递给 function? - How to keep a raw string (as a variable) so that I can pass it to a function? 如何在Python 3中将函数名称作为String传递 - How to pass function name as String in Python 3 如何将当前变量值传递给python函数? - How can I always pass the current variable value into a python function? (Python)我有一个表示现有全局变量名称的字符串,并希望将此字符串传递给函数 - (Python) I have a string that represents an existing global variable's name, and would like to pass this to a function 如何在Python中调用带有存储为字符串的变量的函数 - How in Python can I call a function with a variable which is store as a string 如何通过(字符串)名称 select 变量? - How can I select a variable by (string) name? 我可以将包含函数的所有参数的变量传递给python中的函数吗? - Can i pass a variable which contains all parameters for a function to a function in python?If yes, then how? 如何将变量传递给 function 而无需继续使用在 Python 中调用的原始 function? - How can I pass a variable to a function without continuing with the original function it was called from in Python? Python:如何仅将一个变量传递给lambda函数? - Python: How to pass in only one variable into a lambda function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM