位置参数vs关键字参数

q09*_*987 63 python

基于

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

关键字参数后跟一个等号和一个给出其默认值的表达式.

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

print rectangleArea(width=1, height=2)
Run Code Online (Sandbox Code Playgroud)

提问 >我假定这两个widthheight的位置参数.那么为什么我们也可以用关键字真实参数语法来调用呢?

pax*_*blo 120

您引用的文本是用于函数的定义,与调用函数无关.在对该函数的调用中,您使用的是"命名参数"功能.您提供的链接质量不是很好,作者似乎对两种不同的东西感到困惑.

Python引用仅涉及对函数的调用的位置和关键字参数(请参阅参考资料部分5.3.4 Calls).

当他们谈论部分中7.6 Function definitions函数的定义时,它是一个完全不同的术语"默认参数值".

我怀疑组装该课程的人并不完全熟悉Python :-)


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

def fn (a, b, c = 1):          # a/b required, c optional.
    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.
Run Code Online (Sandbox Code Playgroud)

=变化的含义取决于它是在定义中还是在调用中.

在定义中,它将参数标记为可选,并设置默认值.

在调用中,它只是允许您以任何您想要的顺序指定哪些参数应该是哪些值.

  • 在这种情况下,“命名”等于“关键字”。 (4认同)
  • @Milan(和Fredrick),当Python使用“关键字”时,我可能不应该使用“命名” - 我现在已经解决了这个问题。就何时选择位置/关键字而言,这发生在调用中(而不是定义中),并且取决于您是否使用“fn(10)”或“fn(param=10)”等因素。在定义中,“=”的存在与位置/关键字方面无关。 (2认同)

Naz*_*hal 58

由于 python 3.8 只引入了位置参数,这篇文章需要更新。

位置参数、关键字参数、必需参数和可选参数经常被混淆。位置参数必需参数不同。和关键字参数可选参数不同。

位置参数是可以通过它们在函数定义中的位置来调用的参数。

关键字参数是可以通过名称调用的参数。

必需参数是必须传递给函数的参数。

可选参数是不能传递给函数的参数。在 python 中,可选参数是具有默认值的参数

可选的位置参数(python 3.8)

def f(a=2, /):
    pass


f()  # Allowed, argument is optional
f(1)  # Allowed, it's a positional argument
f(a=1)  # Error, positional only argument
Run Code Online (Sandbox Code Playgroud)

需要的位置参数(python 3.8)

def f(a, /):
    pass


f()  # Error, argument required
f(1)  # Allowed, it's a positional argument
f(a=1)  # Error, positional only argument
Run Code Online (Sandbox Code Playgroud)

可选的关键字参数

def f(*, a=1):
    pass


f()  # Allowed
f(1)  # Error, keyword only arguments
f(a=1)  # Allowed, it's a keyword argument
Run Code Online (Sandbox Code Playgroud)

必需的关键字参数

def f(*, a)
    pass


f()  # Error, argument required
f(1)  # Error, keyword only arguments
f(a=1)  # Allowed, it's a keyword argument
Run Code Online (Sandbox Code Playgroud)

可选的位置和关键字参数

def f(a=1)
    pass


f()  # Allowed, argument is optional
f(1)  # Allowed, it's a positional argument
f(a=1)  # Allowed, it's a keyword argument

# In fact this function is the same as
def f(*, a=1, /):
    pass
Run Code Online (Sandbox Code Playgroud)

需要的位置和关键字参数

def f(a):
    pass


f()  # Error, argument required
f(1)  # Allowed, it's a positional argument
f(a=1)  # Allowed, it's a keyword argument

# In fact this function is the same as
def f(*, a, /):
    pass
Run Code Online (Sandbox Code Playgroud)

结论,一个参数可以是可选的或必需的,不能同时是两者。它也可以是位置、关键字或同时是两者

Python 3.8 引入了仅位置参数

def f(positional_argument, /, positional_or_keyword_argument, *, keyword_argument):
    pass
Run Code Online (Sandbox Code Playgroud)

  • 在最后两个示例中,您的意思是“f(/,a,*)”而不是“f(*, a,/)”吗? (3认同)
  • 你的函数中的“/”是什么,请解释一下。 (3认同)
  • @Lord-shiv 它表示仅位置参数的结束,这些参数不能用作关键字参数。根据/sf/ask/1731471801/ (2认同)

chr*_*htz 17

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


Raf*_*ael 11

首先,参数是函数/方法定义指定参数的命名实体。参数是传递给函数的值

例如,

def rectangle_area(height, width):
    pass

rectangle_area(argument_1, argument_2)
Run Code Online (Sandbox Code Playgroud)

height, width是函数参数,argument_1, argument_2是传递给函数的参数。当你说位置参数时,你正在谈论参数,这与函数定义无关。widthheight是(Python 中默认)位置参数关键字参数(所谓的位置或关键字参数)。因此,您可以按位置或按关键字传递参数。

您如何调用/将值传递给函数决定了它们是位置参数还是关键字参数

对于该函数,rectangle_area我们可以像这样调用它:

rectangle_area(1, 2) # positional arguments
rectangle_area(width=2, height=1) # keyword arguments
Run Code Online (Sandbox Code Playgroud)
  • 在第一次调用中,我们按位置传递值:1 传递给高度,2 传递给宽度。也就是说,当您说1, 2我们的意思是高度为 1 且宽度为 2 时,Python 根据它们传递的位置推断出它们(即,在函数定义中,第一个参数是height,第二个参数是width)。
  • 在第二次调用中,我们通过关键字传递值。我们向 Python 暗示我们要将参数传递给哪个参数。在第二个示例中,我们翻转了参数的顺序,但我们告诉 Python 高度仍然是 1,宽度仍然是 2。两个调用的结果完全相同。

仅位置和仅关键字

很多人不知道的是,您可以通过使用/参数列表中的 来指定仅位置参数(示例来自此处)。

def func(positional_only1, positional_only2, /, positional_or_keyword): ...
Run Code Online (Sandbox Code Playgroud)

同样,您还可以通过使用字符来获得仅关键字参数*

def func(positional_or_keyword, *, keyword_only1, keyword_only2): ...
Run Code Online (Sandbox Code Playgroud)

最后,我们还有 var-positional 和 var-keyword(分别称为 *args 和 **kwargs)。这意味着,您可以将任意序列的位置参数或关键字参数传递给函数。