位置参数v.关键字参数。

时间:2021-04-10 23:22:00

Based on this

在此基础上

A positional argument is a name that is not followed by an equal sign (=) and default value.

位置参数是一个名称,它后面没有一个等号(=)和默认值。

A keyword argument is followed by an equal sign and an expression that gives its default value.

一个关键字参数后面是一个等号和一个表示其默认值的表达式。

def rectangleArea(width, height):
    return width * height

print rectangleArea(width=1, height=2)

Question> I assume that both width and height are positional arguments. Then why we can also call it with keyword real argument syntax?

问题>我假设宽度和高度都是位置参数。那么为什么我们还可以用关键字实参语法来调用它呢?

3 个解决方案

#1


71  

That text you quote is for the definition of the function and has nothing to do with calls to the function. In the call to that function, you're using the "named argument" feature. That link you provide is not a very good quality one, the authors seem confused between two different things.

您引用的文本是函数的定义,与函数调用无关。在调用该函数时,您使用的是“命名参数”特性。你提供的链接不是很好,作者似乎混淆了两个不同的东西。

The Python reference refers to positional and keyword arguments only in respect to a call to a function (see section 5.3.4 Calls).

Python引用只涉及到对函数调用的位置和关键字参数(参见第5.3.4节调用)。

When they talk about the definition of a function in section 7.6 Function definitions, it's a totally different term "default parameter values".

当他们讨论第7.6节函数定义中函数的定义时,这是一个完全不同的术语“默认参数值”。

I suspect the people who put together that course-ware weren't totally familiar with Python :-)

我怀疑那些把课程放在一起的人并不完全熟悉Python:-)


By way of example, refer to the following definition and calls:

举例来说,请参考以下定义和调用:

def fn (a, b, c = 1):
    return a * b + c

print fn (1, 2)                # returns 3, positional and default.
print fn (1, 2, 3)             # returns 5, positional.
print fn (c = 5, b = 2, a = 2) # returns 9, named.
print fn (b = 2, a = 2)        # returns 5, named and default.
print fn (5, c = 2, b = 1)     # returns 7, positional and named.
print fn (8, b = 0)            # returns 1, positional, named and default.

The meaning of the = changes, depending on whether it's in the definition or in the call.

=更改的含义,取决于它是在定义中还是在调用中。

In the definition, it marks the argument optional and sets a default value.

在定义中,它标记了可选参数并设置了默认值。

In the call, it simply allows you to specify which arguments should be which values, in whatever order you want.

在调用中,它只允许您指定哪个参数应该是哪个值,按照您想要的顺序。

#2


9  

A keyword argument is just a positional argument with a default value. You must specify all arguments that don't have a default value. In other words, keyword arguments are only "optional" because they will be set to their default value if not specifically supplied.

关键字参数只是带有默认值的位置参数。您必须指定没有默认值的所有参数。换句话说,关键字参数只是“可选的”,因为如果没有特别提供,它们将被设置为默认值。

#3


2  

Positional arguments can be called either using values in order or by naming each. For example, all three of the following would work the same way:

位置参数可以按顺序调用,也可以通过命名每个参数来调用。例如,以下三种方法的工作方式相同:

def rectangleArea(width, height):
    return width * height

print(rectangleArea(1, 2))
print(rectangleArea(width=1, height=2))
print(rectangleArea(height=2, width=1))

#1


71  

That text you quote is for the definition of the function and has nothing to do with calls to the function. In the call to that function, you're using the "named argument" feature. That link you provide is not a very good quality one, the authors seem confused between two different things.

您引用的文本是函数的定义,与函数调用无关。在调用该函数时,您使用的是“命名参数”特性。你提供的链接不是很好,作者似乎混淆了两个不同的东西。

The Python reference refers to positional and keyword arguments only in respect to a call to a function (see section 5.3.4 Calls).

Python引用只涉及到对函数调用的位置和关键字参数(参见第5.3.4节调用)。

When they talk about the definition of a function in section 7.6 Function definitions, it's a totally different term "default parameter values".

当他们讨论第7.6节函数定义中函数的定义时,这是一个完全不同的术语“默认参数值”。

I suspect the people who put together that course-ware weren't totally familiar with Python :-)

我怀疑那些把课程放在一起的人并不完全熟悉Python:-)


By way of example, refer to the following definition and calls:

举例来说,请参考以下定义和调用:

def fn (a, b, c = 1):
    return a * b + c

print fn (1, 2)                # returns 3, positional and default.
print fn (1, 2, 3)             # returns 5, positional.
print fn (c = 5, b = 2, a = 2) # returns 9, named.
print fn (b = 2, a = 2)        # returns 5, named and default.
print fn (5, c = 2, b = 1)     # returns 7, positional and named.
print fn (8, b = 0)            # returns 1, positional, named and default.

The meaning of the = changes, depending on whether it's in the definition or in the call.

=更改的含义,取决于它是在定义中还是在调用中。

In the definition, it marks the argument optional and sets a default value.

在定义中,它标记了可选参数并设置了默认值。

In the call, it simply allows you to specify which arguments should be which values, in whatever order you want.

在调用中,它只允许您指定哪个参数应该是哪个值,按照您想要的顺序。

#2


9  

A keyword argument is just a positional argument with a default value. You must specify all arguments that don't have a default value. In other words, keyword arguments are only "optional" because they will be set to their default value if not specifically supplied.

关键字参数只是带有默认值的位置参数。您必须指定没有默认值的所有参数。换句话说,关键字参数只是“可选的”,因为如果没有特别提供,它们将被设置为默认值。

#3


2  

Positional arguments can be called either using values in order or by naming each. For example, all three of the following would work the same way:

位置参数可以按顺序调用,也可以通过命名每个参数来调用。例如,以下三种方法的工作方式相同:

def rectangleArea(width, height):
    return width * height

print(rectangleArea(1, 2))
print(rectangleArea(width=1, height=2))
print(rectangleArea(height=2, width=1))