函数参数Python

2024-09-27 20:19:58 发布

您现在位置:Python中文网/ 问答频道 /正文

def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
    print "-- This parrot wouldn’t", action
    print "if you put", voltage, "volts through it."
    print "-- Lovely plumage, the", type
    print "-- It’s", state, "!"

我开始学习python。我可以使用parrot(5,'dead')和parrot(voltage=5)调用这个函数。但是为什么我不能用parrot的相同函数调用(voltage=5,'dead')?在


Tags: ifdeftypeactionbluethisparrotstate
1条回答
网友
1楼 · 发布于 2024-09-27 20:19:58

不能在关键字参数(arg_name='arg_value')之后使用非关键字参数('arg_value')。这是因为Python是如何设计的。在

请看这里:http://docs.python.org/tutorial/controlflow.html#keyword-arguments

因此,必须输入关键字参数后面的所有参数作为关键字参数。。。在

# instead of parrot(voltage=5, 'dead'):
parrot(voltage=5, state='dead')

# or:
parrot(5, state='dead')

# or:
parrot(5, 'dead')

相关问题 更多 >

    热门问题