位置参数与关键字参数

基于 这个

位置参数是不后跟等号的名称 (=)和默认值。

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

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


    

print rectangleArea(width=1, height=2)

提问。我假设 widthheight都是位置参数。那么为什么我们还可以用关键字参数语法来调用它呢?

212623 次浏览

你引用的那段文字似乎对两件完全不同的事情感到困惑:

我怀疑制作这些课件的人并不熟悉 Python: ——因此,你提供的链接质量并不高。


在函数的 打电话中,使用了“关键字参数”特性(在这里参数是命名的,而不是依赖于它的位置)。如果没有,则仅根据顺序将值绑定到名称。因此,在这个例子中,下面的两个调用是等价的:

def process_a_and_b(a, b):
blah_blah_blah()


process_a_and_b(1, 2)
process_a_and_b(b=2, a=1)

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

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.

位置参数可以按顺序使用值来调用,也可以通过命名每个参数来调用。例如,以下三种方法的工作原理是一样的:

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

假设,我们通过了“来”第一,“好”第二,然后结果会是“好”。此外,调用函数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.

位置参数、关键字参数、必需参数和可选参数常常混淆。位置参数 是不一样的要求参数,关键字参数 是不一样的可选参数。

位置(- only)参数 绑定到调用中提供的位置参数,即位置参数。

关键字(- only)参数 绑定到调用中提供的关键字参数,即通过名称。

位置或关键字参数 绑定到调用中提供的位置参数或关键字参数,即通过位置或名称。

必需的参数 绑定到调用中提供的参数。

可选参数 绑定到定义中提供的默认参数。

This is the Python syntax for declaring parameters:

def f(positional_parameter, /, positional_or_keyword_parameter, *, keyword_parameter):
pass
  • 所需的位置参数(自 Python 3.8以来) :

    def f(a, /):
    pass
    
    
    
    
    f()  # error, argument is required
    f(1)  # allowed, positional argument
    f(a=1)  # error, keyword argument
    
  • 可选的位置参数(自 Python 3.8以来) :

    def f(a=2, /):
    pass
    
    
    
    
    f()  # allowed, argument is optional
    f(1)  # allowed, positional argument
    f(a=1)  # error, keyword argument
    
  • 所需的关键字参数:

    def f(*, a):
    pass
    
    
    
    
    f()  # error, argument is required
    f(1)  # error, positional argument
    f(a=1)  # allowed, keyword argument
    
  • Keyword parameter that is optional:

    def f(*, a=1):
    pass
    
    
    
    
    f()  # allowed, argument is optional
    f(1)  # error, positional argument
    f(a=1)  # allowed, keyword argument
    
  • 所需的位置或关键字参数:

    def f(a):
    pass
    
    
    
    
    f()  # error, argument is required
    f(1)  # allowed, positional argument
    f(a=1)  # allowed, keyword argument
    
    
    
    
    # In fact that function is the same as this one.
    
    
    def f(/, a, *):
    pass
    
  • Positional-or-keyword parameter that is optional:

    def f(a=1):
    pass
    
    
    
    
    f()  # allowed, argument is optional
    f(1)  # allowed, positional argument
    f(a=1)  # allowed, keyword argument
    
    
    
    
    # In fact that function is the same as this one.
    
    
    def f(/, a=1, *):
    pass
    

结论: ー参数可以是必需的或可选的,但 不是同时发生的除外。它也可以是位置、关键字或 两者同时发生

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

比如说,

def rectangle_area(height, width):
pass


rectangle_area(argument_1, argument_2)

height, width是函数参数,argument_1, argument_2是传递给函数的参数。当你说到位置参数时,你说的是 arguments,这和函数定义没有关系。widthheight是(在 Python 中默认为)位置参数 或者关键字参数(所谓的位置或关键字参数)。因此,可以按位置传递参数,也可以按关键字传递参数。

如何调用/向函数传递值决定了它们是位置 争论还是关键字 争论

对于函数 rectangle_area,我们可以这样称呼它:

rectangle_area(1, 2) # positional arguments
rectangle_area(width=2, height=1) # keyword arguments
  • 在第一个调用中,我们将值 位置上:1传递给高度,2传递给宽度。也就是说,Python 推断当你说 1, 2时,我们的意思是高度是1,宽度是2,这取决于它们被传递的位置(也就是说,在函数定义中,第一个参数是 height,第二个是 width)。
  • 在第二个调用中,我们通过 keywords传递值。我们向 Python 暗示要将参数传递给哪个参数。在第二个示例中,我们翻转参数的顺序,但是我们告诉 Python 高度仍然是1,宽度仍然是2。两种调用的结果完全相同。

仅位置和只关键字

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

def func(positional_only1, positional_only2, /, positional_or_keyword): ...

类似地,还可以通过使用 *字符来拥有仅关键字参数。

def func(positional_or_keyword, *, keyword_only1, keyword_only2): ...

最后,我们还有 var 位置关键字和 var 关键字(分别是 a.k.a * args 和 * * kwargs)。也就是说,可以将任意序列的位置参数或关键字参数传递给函数。

我假设 width 和 height 都是位置参数。那么为什么我们还可以用关键字参数语法来调用它呢?

为了防止只能使用位置参数:

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


print rectangleArea(width=1, height=2)

错误消息如下:

输入错误: rectangleArea ()获得了一些仅作为关键字参数传递的位置参数: ‘ width,height’

Here is some extra information to complete @Nazime Lakehal’s 答得好.

可选的位置参数后面不能跟必需的位置参数或位置或关键字参数:

# SyntaxError.


def f(a=1, b, /):
pass




# SyntaxError.


def f(a=1, /, b):
pass

可选的位置或关键字参数后面不能跟必需的位置或关键字参数:

# SyntaxError.


def f(a=1, b):
pass

For binding a positional parameter that is optional, all previous positional parameters that are optional have to be bound, making them all effectively required. That may be the origin of the confusion of positional parameters with required parameters:

def f(a=1, b=2, /):
pass




f(1, 0)

对于绑定可选的关键字参数或位置或关键字参数,不必绑定所有其他可选的关键字参数和位置或关键字参数。这可能是关键字参数与可选参数混淆的原因:

def f(c=3, *, a=1, b=2):
pass




f(b=0)




def f(a=1, b=2, *, c=3):
pass




f(b=0)