基于此
位置参数是一个名称,后面没有等号(=)和默认值.
关键字参数后跟一个等号和一个给出其默认值的表达式.
def rectangleArea(width, height):
return width * height
print rectangleArea(width=1, height=2)
Run Code Online (Sandbox Code Playgroud)
提问 >我假定这两个width
和height
的位置参数.那么为什么我们也可以用关键字真实参数语法来调用呢?
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)
=
变化的含义取决于它是在定义中还是在调用中.
在定义中,它将参数标记为可选,并设置默认值.
在调用中,它只是允许您以任何您想要的顺序指定哪些参数应该是哪些值.
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)
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
是传递给函数的参数。当你说位置参数时,你正在谈论参数,这与函数定义无关。width
和height
是(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 时,Python 根据它们传递的位置推断出它们(即,在函数定义中,第一个参数是height
,第二个参数是width
)。很多人不知道的是,您可以通过使用/
参数列表中的 来指定仅位置参数(示例来自此处)。
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)。这意味着,您可以将任意序列的位置参数或关键字参数传递给函数。
归档时间: |
|
查看次数: |
97893 次 |
最近记录: |