Function Parameters - 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, "!" 

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

+8
python
source share
1 answer

You cannot use an argument without a keyword ( 'arg_value' ) after a keyword argument ( arg_name='arg_value' ). This is due to how Python is designed.

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

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

 # instead of parrot(voltage=5, 'dead'): parrot(voltage=5, state='dead') # or: parrot(5, state='dead') # or: parrot(5, 'dead') 
+13
source share

All Articles