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')?
Function Arguments Parameters in python are variables — placeholders for the actual values the function needs. When the function is called, these values are passed in as arguments. For example, the arguments passed into the function .
Function parameters are the names listed in the function's definition. Function arguments are the real values passed to the function. Parameters are initialized to the values of the arguments supplied.
You can use inspect. getargspec() to see what arguments are accepted, and any default values for keyword arguments. inspect. getargspec() should be considered deprecated in Python 3.
5 Types of Arguments in Python Function Definition:keyword arguments. positional arguments. arbitrary positional arguments. arbitrary keyword arguments.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With