Introduction
What are parameters and arguments? Most of the time we use both the terms interchangeably. And that should be fine. However, it’s important that you understand the difference between the two. In this article, let’s explore both in detail. Along the way, you will learn all you need to know about parameters & arguments in Python. Let’s jump right in.
Parameters & Arguments
Parameters
Parameters are the names that appear in the function definition. In the below example, name, age, and skill are the parameters as they appear in the function definition.
def my_func(name, age, skill):
pass
Arguments
Arguments are the names that appear in the function call. In the above example, ‘Chetan’, 33, and ‘Python’ are the arguments as they appear when making a call to the function my_func.
my_func('Chetan', 33, 'Python')
Types of arguments
There are two types of arguments. Let’s explore the syntax now and then in the next section you will understand how it works with examples.
Keyword Arguments
Keyword argument is preceded by identifier/variable in the function call. In the above example, “Chetan” and 33 are the keyword arguments; as you can see they are preceded by identifiers name and age. Keyword arguments follow key=value
pairs syntax. It is also called named arguments.
def my_func(name, age):
pass
my_func(name='Chetan', age=33)
Positional Arguments
Any argument that is not a keyword argument or that doesn’t precede with an identifier is a positional argument. In the below example, chetan and 33 are the positional arguments as they don’t precede by any identifier.
def my_func(name, age):
pass
my_func('Chetan', 33)
Types of Parameters
There are 5 different types of parameters. Let’s explore the syntax and understand how it works with examples.
Positional or keyword
Indicates arguments that can be passed to a function either positionally or as a keyword argument. The positional parameters come first followed by keyword parameters. In the below syntax, pos1 is positional and key1 is the keyword parameter.
def func(pos1, key1=None):
pass
Python’s built-in function print() makes use of positional or keyword parameter as you can see from the below function definition.
Positional-only
Positional-only parameters indicate that arguments can be passed to function only by position. You can create positional-only parameters using /
symbol. All the parameters that come before /
are strictly positional-only parameters.
In the below syntax, pos_only1 and pos_only2 are positional-only meaning during the function call we won’t be able to use them as keyword arguments like pos_only1=10, etc. The function call would like to func(10, 20, 30) where 10, 20 correspond to pos_only1 & pos_only2 and 30 can be either positional and keyword parameter.
def func(pos_only1, pos_only2, /, positional_or_keyword):
pass
Python’s built-in function len() makes use of positional-only parameter as you can see the function definition in the below screenshot.
Keyword-only
Keyword-only parameters indicate that arguments can only be passed to function by keywords. You can create keyword-only parameters using *
symbol. All the parameters that come after *
are strictly keyword-only parameters.
In the below syntax, pos_only1 & pos_only2 are positional parameters and key_only & key_only2 are keyword-only parameters. This means that both key_only1 & key_only2 can only be used as keyword arguments in the function call.
def func(pos_only1, pos_only2, *, key_only1, key_only2):
pass
Python’s built-in function sorted() makes use of both keyword-only and positional-only parameters as you can see the function definition below:
Var-positional
This parameter is useful when your application requires an arbitrary number of positional parameters. This is done by appending *
to the parameter. The *args holds an arbitrary number of remaining positional arguments. Note that the use of the variable args is not mandatory. You could use any variable name but args has become the defacto. The args return its values as a tuple.
def func(*args):
pass
Python’s built-in function max() makes use of Var-positional parameter *args as you can see from the function definition.
Var-keyword
This parameter is useful when your application requires an arbitrary number of keyword parameters. This is done by appending **
to the parameter. The *kwargs hold an arbitrary number of remaining keyword arguments. Similar to args, the name of the variable kwargs is also not mandatory. The **kwargs returns a dictionary containing parameters and their arguments.
def func(**kwargs):
pass
Example
Python’s built-in function dict() is an example Var-keyword parameter. Refer to the function definition below.
The below two examples pretty much sum up everything we learned so far about parameters.
Summary
- Parameters appear in the function definition and arguments appear in the function call.
- There are two types of arguments (positional and keyword arguments) and five types of parameters (positional or keyword, positional-only, keyword-only, Var-positional, and Var-keyword).
- Positional parameters can also have default values which can be specified using keywords.
- All the parameters that precede
/
in the function definition are strictly positional (positional-only). - All the parameters that follow
*
in the function definition are strictly keyword (keyword-only). - The *args holds an arbitrary number of remaining positional arguments.
- The **kwargs hold an arbitrary number of remaining keyword arguments.
- Positional arguments must not follow keyword arguments in the function call.
- No parameter comes after **kwargs in the function definition. It’s the end of all the parameters.