英文:
How do I pass multiple parameter arguments as a single variable to a function in Python
问题
以下是已翻译的代码部分:
**NOTE** 我不创建inner_function - 我正在尝试调用其中的特定函数,而这似乎是最容易的方式来解释它,而不使其特定于我正在使用的库。
这是我要更具体尝试的内容。这将是两个函数:
```python
def inner_function(parameter1='nothing', parameter2='nothing', parameter3='nothing'):
print(parameter1)
print(parameter2)
print(parameter3)
def outer_function(parameter_set=<默认值>):
inner_function(parameter_set)
首先,当我调用outer_function()
时,我期望输出为:
nothing
nothing
nothing
我不知道要实现这个输出,<默认值>
应该是什么。
其次,如果我希望输出为:
nothing
wamo
thunk
那么传递给outer_function()
的参数应该是什么?
对不起,如果这种方式解释或以前已经提出这个问题令人困惑,但我很难找到回答这个问题的帖子。我对默认值没有尝试过太多 - 只有None和空的[]。
英文:
NOTE I am not creating the inner_function - I am trying to call a specific function within mine and this seemed to be the easiest way to explain it without making is specific to the library I am using.
Here is what I am trying to do more specifically. This will be the two functions:
def inner_function(parameter1='nothing', parameter2='nothing', parameter3='nothing'):
print(parameter1)
print(parameter2)
print(parameter3)
def outer_function(parameter_set=<default value>):
inner_function(parameter_set)
First, when I call outer_function()
I expect the output to be:
nothing
<br>nothing
<br>nothing
I do not know what the <default value>
should be to accomplish this.
Second, if I want the output to be:
nothing
<br>wamo
<br>thunk
What would the argument passed into outer_function()
be?
Sorry if this is explained in a confusing manner or has been asked before but I am having trouble finding a post answering this.
I have not tried much for the default value - only None and empty []
答案1
得分: 1
你可以使用关键字参数,如评论中所说:
def inner_function(parameter1='nothing', parameter2='nothing', parameter3='nothing'):
print(parameter1)
print(parameter2)
print(parameter3)
def outer_function(parameter_set={}):
inner_function(**parameter_set)
一些测试:
>>> k1 = {"parameter2": "foo"}
>>> k2 = {
... "parameter1": "YEs",
... "parameter2": False,
... "parameter3": (1,2,3),
... }
>>> inner_function()
nothing
nothing
nothing
>>> outer_function()
nothing
nothing
nothing
>>> outer_function(k1)
nothing
foo
nothing
>>> outer_function(k2)
YEs
False
(1, 2, 3)
请记住,kwargs
变量可以传递任何内容。如果要限制它只接受你的变量,你应该在运行 inner_function
之前在 outer_function
中添加一些代码来检查它。
英文:
As someone said in the comments, you can use keyword arguments:
def inner_function(parameter1='nothing', parameter2='nothing', parameter3='nothing'):
print(parameter1)
print(parameter2)
print(parameter3)
def outer_function(parameter_set={}):
inner_function(**parameter_set)
Some tests:
>>> k1 = {"parameter2": "foo"}
>>> k2 = {
... "parameter1": "YEs",
... "parameter2": False,
... "parameter3": (1,2,3),
... }
>>> inner_function()
nothing
nothing
nothing
>>> outer_function()
nothing
nothing
nothing
>>> outer_function(k1)
nothing
foo
nothing
>>> outer_function(k2)
YEs
False
(1, 2, 3)
Just remember that anything can get passed in the kwargs
variable, if you want to limit it to your variables, you should add some code in outer_function
to check it before running inner_function
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论