Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function parameters - Python

Tags:

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 parrot(5,'dead') and parrot(voltage=5). But why can't I call with the same function with parrot(voltage=5, 'dead')?

like image 286
Ramana Venkata Avatar asked Dec 27 '11 06:12

Ramana Venkata


People also ask

What are function parameters in Python?

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 .

What are function parameters?

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.

How do you check parameters of a function in Python?

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.

What kind of parameters do functions have in Python?

5 Types of Arguments in Python Function Definition:keyword arguments. positional arguments. arbitrary positional arguments. arbitrary keyword arguments.


1 Answers

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')
like image 184
poplitea Avatar answered Oct 18 '22 10:10

poplitea