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)调用此函数。但是,为什么不能用鹦鹉的同一个函数调用(电压= 5,“死”)?
最佳答案
您不能在关键字参数('arg_value'
)后使用非关键字参数(arg_name='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')