基于此
位置参数是一个名称,后跟等号(=)和默认值。
关键字参数后跟一个等号和一个给出其默认值的表达式。
def rectangleArea(width, height):
return width * height
print rectangleArea(width=1, height=2)
问题>我认为width和height都是位置参数。那为什么还要用关键字参数语法来调用它呢?
Answers:
您引用的文本用于函数的定义,与函数的调用无关。在对该函数的调用中,您使用的是“命名参数”功能。您提供的链接质量不是很好,作者似乎对两种不同的事物感到困惑。
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.
=更改的含义取决于它是在定义中还是在调用中。
在定义中,它将参数标记为可选,并设置默认值。
在调用中,它仅允许您以所需顺序指定哪些参数应为哪些值。
由于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
所需的位置参数(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
关键字参数,是可选的
def f(*, a=1):
pass
f() # Allowed
f(1) # Error, keyword only arguments
f(a=1) # Allowed, it's a keyword argument
必需的关键字参数
def f(*, a)
pass
f() # Error, argument required
f(1) # Error, keyword only arguments
f(a=1) # Allowed, it's a keyword argument
位置和关键字参数,是可选的
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
所需的位置和关键字参数
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
结论,参数可以是可选的,也可以不是同时需要的。它也可以是位置,关键字或同时使用。
Python 3.8引入了仅位置参数。
def f(positional_argument, /, positional_or_keyword_argument, *, keyword_argument):
pass
f(/,a,*)不是f(*, a, /)在最后两个示例中?
可以使用值顺序或通过命名每个位置参数。例如,以下所有三个将以相同的方式工作:
def rectangleArea(width, height):
return width * height
print(rectangleArea(1, 2))
print(rectangleArea(width=1, height=2))
print(rectangleArea(height=2, width=1))
位置参数:以正确的位置顺序传递给函数的参数。下面的程序了解函数的位置参数
#positional arguments example
def combine(str1, str2):
#To join str1 and str2 with str3
str3 = str1 + str2
print(str3)
#call combine() and pass 2 strings
combine("Well", "come") #positional arguments
假设,我们先通过“ come”,然后通过“ well”,那么结果将为Comewell。另外,调用函数3字符串会出错。
了解函数的关键字参数。
关键字参数是通过名称来标识参数的参数。
#keyword arguments example:
def employee(name, Id):
print("Employee Name: ", name)
print("Employee Id : ", Id)
#call employee() and pass 2 arguments
employee(name = "inban", Id = "pay001")
employee(Id = "pay002", name = "karthik") #we can change the order args.
在此处定义参数和参数可能会有所帮助。
例如,
def my_function(parameter_1, parameter_2):
pass
my_function(argument_1, argument_2)
现在,当您说位置参数时,您是在谈论arguments,因此与函数定义无关。width并且height在你的例子是位置参数或关键字参数(所谓的位置有或关键字参数)。
如何调用/将值传递给函数将确定它们是位置参数还是关键字参数。
rectangleArea(1, 2) # positional arguments
rectangleArea(width=1, height=2) # keyword arguments
很少有人知道的是,您可以通过使用/参数列表中的来指定仅位置参数(示例来自此处)。
def func(positional_only1, positional_only2, /, positional_or_keyword): ...
同样,您也可以通过使用*字符来使用仅关键字参数。
def func(positional_or_keyword, *, keyword_only1, keyword_only2): ...
最后,我们还有var-positional和var-keyword(分别为* args和** kwargs)。意思是,您可以将任意位置参数或关键字参数序列传递给该函数。