“name = value”类型的Python函数参数的名称和原因

时间:2022-05-07 10:07:51

It's entirely possible that this question is a duplicate, but I don't know what this concept is called so I don't even know how to search for it.

完全有可能这个问题是重复的,但我不知道这个概念是什么,所以我甚至不知道如何搜索它。

I'm new to Python and trying to understand this function from a Caffe example:

我是Python的新手并试图从Caffe示例中理解这个函数:

def conv_relu(bottom, ks, nout, stride=1, pad=0, group=1):
    conv = L.Convolution(bottom, kernel_size=ks, stride=stride,
                                num_output=nout, pad=pad, group=group)
    return conv, L.ReLU(conv, in_place=True)

I figured the parameters stride=1, pad=1, etc in the conv_relu function definition are default initial values, but then what do kernel_size=ks, stride=stride, etc in the L.Convolution call mean? Is it kind of like a name/value pair?

我认为conv_relu函数定义中的参数stride = 1,pad = 1等是默认初始值,但是L.Convolution调用中的kernel_size = ks,stride = stride等是什么?它有点像名字/价值对吗?

If nothing else, can someone please tell me what this is called?

如果没有别的,有人可以告诉我这叫什么?

3 个解决方案

#1


6  

Those are keyword arguments.

这些是关键字参数。

some_function(x=y, z=zz) 

x is the name of the parameter when the function was declared, and y is the value that's being passed in.

x是声明函数时参数的名称,y是传入的值。

Reasons to use keyword arguments:

使用关键字参数的原因:

  • You can give them in any order, instead of only the order in the function definition
  • 您可以按任何顺序给出它们,而不仅仅是函数定义中的顺序

  • When you look back on the code later, if the parameters have good names, you can immediately tell the purpose of the passed variables instead of needing to check the function definition or documentation to see what the data means.
  • 当您稍后回顾代码时,如果参数具有良好的名称,您可以立即告知传递的变量的目的,而不是需要检查函数定义或文档以查看数据的含义。

#2


5  

You have a call expression using keyword arguments. Each name=value pair assigns a value to a specific parameter the function accepts.

您有一个使用关键字参数的调用表达式。每个name = value对都为函数接受的特定参数赋值。

Keyword arguments can be used in any order. If the named parameter in the function signature has a default value, the value in the call overrides that default. If you omit a specific argument, the default value is used.

关键字参数可以按任何顺序使用。如果函数签名中的命名参数具有默认值,则调用中的值将覆盖该默认值。如果省略特定参数,则使用默认值。

#3


4  

In Python, arguments can be provided by position, or by keyword.

在Python中,参数可以通过position或关键字提供。

For example, say you have the following function:

例如,假设您具有以下功能:

def foo(bar, baz=1, qux=2):
    pass

You can call it as foo(3), so in the callee scope, bar would be 3. baz and qux will be assigned their default values - 1 and 2, respectively - so you don't have to provide them explicitly.

您可以将其称为foo(3),因此在被调用者范围中,bar将为3. baz和qux将分别分配其默认值 - 1和2 - 因此您不必明确提供它们。

You can also call this function using keyword arguments (that's the term you were searching for). The exact same call with bar as a named argument would be foo(bar=3).

您也可以使用关键字参数调用此函数(这是您要搜索的术语)。使用bar作为命名参数的完全相同的调用将是foo(bar = 3)。

Of course, we would rather just use foo(3) since it's more concise, unless there are specific reasons to use named arguments. An example of such reason is if we wish to provide a non-default argument for qux, while leaving the default argument for baz unchanged: foo(3, qux=4).

当然,我们宁愿只使用foo(3),因为它更简洁,除非有特定的理由使用命名参数。这种原因的一个例子是,如果我们希望为qux提供非默认参数,同时保持baz的默认参数不变:foo(3,qux = 4)。

#1


6  

Those are keyword arguments.

这些是关键字参数。

some_function(x=y, z=zz) 

x is the name of the parameter when the function was declared, and y is the value that's being passed in.

x是声明函数时参数的名称,y是传入的值。

Reasons to use keyword arguments:

使用关键字参数的原因:

  • You can give them in any order, instead of only the order in the function definition
  • 您可以按任何顺序给出它们,而不仅仅是函数定义中的顺序

  • When you look back on the code later, if the parameters have good names, you can immediately tell the purpose of the passed variables instead of needing to check the function definition or documentation to see what the data means.
  • 当您稍后回顾代码时,如果参数具有良好的名称,您可以立即告知传递的变量的目的,而不是需要检查函数定义或文档以查看数据的含义。

#2


5  

You have a call expression using keyword arguments. Each name=value pair assigns a value to a specific parameter the function accepts.

您有一个使用关键字参数的调用表达式。每个name = value对都为函数接受的特定参数赋值。

Keyword arguments can be used in any order. If the named parameter in the function signature has a default value, the value in the call overrides that default. If you omit a specific argument, the default value is used.

关键字参数可以按任何顺序使用。如果函数签名中的命名参数具有默认值,则调用中的值将覆盖该默认值。如果省略特定参数,则使用默认值。

#3


4  

In Python, arguments can be provided by position, or by keyword.

在Python中,参数可以通过position或关键字提供。

For example, say you have the following function:

例如,假设您具有以下功能:

def foo(bar, baz=1, qux=2):
    pass

You can call it as foo(3), so in the callee scope, bar would be 3. baz and qux will be assigned their default values - 1 and 2, respectively - so you don't have to provide them explicitly.

您可以将其称为foo(3),因此在被调用者范围中,bar将为3. baz和qux将分别分配其默认值 - 1和2 - 因此您不必明确提供它们。

You can also call this function using keyword arguments (that's the term you were searching for). The exact same call with bar as a named argument would be foo(bar=3).

您也可以使用关键字参数调用此函数(这是您要搜索的术语)。使用bar作为命名参数的完全相同的调用将是foo(bar = 3)。

Of course, we would rather just use foo(3) since it's more concise, unless there are specific reasons to use named arguments. An example of such reason is if we wish to provide a non-default argument for qux, while leaving the default argument for baz unchanged: foo(3, qux=4).

当然,我们宁愿只使用foo(3),因为它更简洁,除非有特定的理由使用命名参数。这种原因的一个例子是,如果我们希望为qux提供非默认参数,同时保持baz的默认参数不变:foo(3,qux = 4)。