keyword arguments
functions can also be called using keyword arguments of the form kwarg=value. for instance, the following function:函数可以用keyword = value的关键字参数形式被调用.例如,以下函数:
def parrot(voltage, state='a stiff', action='voom', type='norwegian blue'):
print(-- this parrot wouldn't, action, end=' ')
print(if you put, voltage, volts through it.)
print(-- lovely plumage, the, type)
print(-- it's, state, !)
functions can also be called using keyword arguments of the form kwarg=value. for instance, the following function:接受一个必需的参数(voltage)和三个可选参数(state、action和type)。这个函数可以用以下任何方式调用:
parrot(1000) # 1 positional argument
parrot(voltage=1000) # 1 keyword argument
parrot(voltage=1000000, action='vooooom') # 2 keyword arguments
parrot(action='vooooom', voltage=1000000) # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump') # 3 positional arguments
parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 keyword
but all the following calls would be invalid: 但下面的是无效的:
parrot() # required argument missing
parrot(voltage=5.0, 'dead') # non-keyword argument after a keyword argument
parrot(110, voltage=220) # duplicate value for the same argument
parrot(actor='john cleese') # unknown keyword argument
in a function call, keyword arguments must follow positional arguments. all the keyword arguments passed must match one of the arguments accepted by the function (e.g. actor is not a valid argument for the parrot function), and their order is not important. this also includes non-optional arguments (e.g. parrot(voltage=1000) is valid too). no argument may receive a value more than once. here’s an example that fails due to this restriction:在函数调用中,关键字参数必须遵循位置参数。所有传递的关键字参数必须匹配函数接受的参数之一(例如,演员不是鹦鹉函数的有效参数),它们的顺序并不重要。这也包括非可选参数(例如,parrot(voltage=1000)也是有效的)。没有一个参数可以不止一次地接收到一个值。这里有一个由于这个限制而失败的例子:
>>> def function(a):
... pass
...
>>> function(0, a=0)
traceback (most recent call last):
file , line 1, in
typeerror: function() got multiple values for keyword argument 'a'
when a final formal parameter of the form **name is present, it receives a dictionary (see mapping types — dict) containing all keyword arguments except for those corresponding to a formal parameter. this may be combined with a formal parameter of the form *name (described in the next subsection) which receives a tuple containing the positional arguments beyond the formal parameter list. (*name must occur before **name.) for example, if we define a function like this:当**name形式的最终形参出现时,它将接收一个字典(查看映射类型-字典)包含除那些相对应的形式参数外的所有关键字参数.这可以和以*name形式的形参结合,该形式形参接收含有超出形参列表的位置参数的元组.(*name必须在**name前.)例如,如果我们定义一个函数如下:
def cheeseshop(kind, *arguments, **keywords):
print(-- do you have any, kind, ?)
print(-- i'm sorry, we're all out of, kind)
for arg in arguments:
print(arg)
print(- * 40)
for kw in keywords:
print(kw, :, keywords[kw])
it could be called like this:将调用成下面的样子
cheeseshop(limburger, it's very runny, sir.,
it's really very, very runny, sir.,
shopkeeper=michael palin,
client=john cleese,
sketch=cheese shop sketch)
and of course it would print:当然会打印成这样
-- do you have any limburger ?
-- i'm sorry, we're all out of limburger
it's very runny, sir.
it's really very, very runny, sir.
----------------------------------------
shopkeeper : michael palin
client : john cleese
sketch : cheese shop sketch
note that the order in which the keyword arguments are printed is guaranteed to match the order in which they were provided in the function call.注意,关键字参数名列表在打印内容前被创建,通过keys()函数获取所有关键字结果并进行排序;否则参数打印顺序未定义.