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

I started learning python. I can call this function using parrot(5,'dead') and parrot(voltage=5). But why can't I call with the same function with parrot(voltage=5, 'dead')?

1 Answer 1

13

You can't use a non-keyword argument ('arg_value') after a keyword argument (arg_name='arg_value'). This is because of how Python is designed.

See here: http://docs.python.org/tutorial/controlflow.html#keyword-arguments

Therefore, you must enter all arguments following a keyword-argument as keyword-arguments...

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

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

# or:
parrot(5, 'dead')
Sign up to request clarification or add additional context in comments.

1 Comment

parrot(5, state='dead') would also be valid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.