-5

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

4

1 回答 1

1

只是为了回答,正如文档中所说(是的,你知道人们不读的东西,因为它很难阅读):

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
于 2013-06-30T16:31:20.930 回答