功能 - Python编程

时间:2013-06-30 16:25:09

标签: python-3.x

是否可以在python中通过关键字发送函数参数,以便参数的顺序无关紧要?如果是这样,有人可以用一个例子来解释我吗?

1 个答案:

答案 0 :(得分:1)

只是为了回答,正如在documentation中所说的那样(是的,你知道人们因为难以阅读而没有阅读的内容):

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, "!")

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