您引用的那段文字似乎对两个完全不同的事情感到困惑:
- 位置参数和关键字参数是calls到一个函数(参见Python 参考部分5.3.4 Calls http://docs.python.org/reference/expressions.html#calls).
- 默认值是函数的一个特征定义, as per section 7.6 Function definitions http://docs.python.org/reference/compound_stmts.html#function-definitions
我怀疑制作课件的人不是totally熟悉 Python :-) 因此,您提供的链接质量不是很好。
In your call对于您的函数,您正在使用“关键字参数”功能(其中参数被命名而不是依赖于其位置)。如果没有它,值将仅根据顺序绑定到名称。因此,在此示例中,以下两个调用是等效的:
def process_a_and_b(a, b):
blah_blah_blah()
process_a_and_b(1, 2)
process_a_and_b(b=2, a=1)
进一步举例,请参考以下定义和调用:
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.