[Tutor] Python question 4.8.2. Keyword Argument

Mats Wichmann mats at wichmann.us
Tue Mar 5 18:16:44 EST 2024


On 3/5/24 10:52, Desiree C .Thomas wrote:

With a signature of:

def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):


> parrot(voltage=5.0, 'dead')
> 
> This is the error I get: SyntaxError: positional argument follows keyword argument on line 10
> 
>  From my understanding, I thought the voltage should accept voltage to be 5.0 and update state into 'dead'? Can I get an explaination for this error.

voltage has no default so it is required, the others are optional.  If 
there are no special indicators in the signature (a lone / for 
positional-only and a lone * for keyword-only), then the original Python 
rules apply, which are that if a parameter is listed with a default it 
is optional and without one it is mandatory.  Positional arguments are 
consumed first, and matched up with the argument order in the signature. 
  Once a keyword argument is seen, all the rest must be keyword 
arguments.  You've violated that last bit: once you did "voltage=5.0" it 
is impossible for there to be any following positional arguments.  It 
would have been ok to say

parrot(voltage=5.0, state='dead')

or

parrot(state='dead', voltage=5.0)

or

parrot(5.0, 'dead')

but once you had a x=y form, no more positional args are accepted. As 
the error message said...




More information about the Tutor mailing list