如何将多个参数参数作为单个变量传递给Python函数

huangapple go评论59阅读模式
英文:

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=&#39;nothing&#39;, parameter2=&#39;nothing&#39;, parameter3=&#39;nothing&#39;):
    print(parameter1)
    print(parameter2)
    print(parameter3)

def outer_function(parameter_set=&lt;default value&gt;):
    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 &lt;default value&gt; 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=&#39;nothing&#39;, parameter2=&#39;nothing&#39;, parameter3=&#39;nothing&#39;):
    print(parameter1)
    print(parameter2)
    print(parameter3)

def outer_function(parameter_set={}):
    inner_function(**parameter_set)

Some tests:

&gt;&gt;&gt; k1 = {&quot;parameter2&quot;: &quot;foo&quot;}
&gt;&gt;&gt; k2 = {
...  &quot;parameter1&quot;: &quot;YEs&quot;,
...  &quot;parameter2&quot;: False,
...  &quot;parameter3&quot;: (1,2,3),
... }
&gt;&gt;&gt; inner_function()
nothing
nothing
nothing
&gt;&gt;&gt; outer_function()
nothing
nothing
nothing
&gt;&gt;&gt; outer_function(k1)
nothing
foo
nothing
&gt;&gt;&gt; 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

huangapple
  • 本文由 发表于 2023年2月6日 09:27:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定