What are keyword arguments in Python?

What are keyword arguments in Python?

To improve the manageability and organization of a program in Python, we use functions to break the code in smaller chunks.

Functions are sets of statements that:

  • take an input

  • process it accordingly and

  • return an output.

They can either be built-in functions, which are part of the Python program, or user-defined functions, which are created using the def keyword.

E.g.,

# Creating a simple function named add()
def add():
    pass

In user-defined functions, values can be passed through the use of arguments (parameters) as illustrated below,

# Passing 2 parameters to my add() function
def add(num1, num2):
    pass

What's an argument ?

An argument could be defined as a value that is sent to a function when it is called. In Python, functions can be defined to use a variable number of arguments. We have three types of arguments:

  • default arguments
  • keyword arguments and
  • arbitrary arguments.

But in this article we will focus on keyword arguments.

Keyword Argument in Python : Definition

A keyword argument is an argument passed to a function or method that is preceded by a keyword and an equals (=) sign. The general form is:

# Passing a keyword argument to a function
function(keyword = value)

Where function is the function name, keyword is the keyword argument and value is the value or object passed as that keyword.

When are keywords arguments used ?

When you have some functions with many arguments and you would like to specify only some of them, you can give values for such arguments by naming them. This is the process of keyword arguments.

How?

Instead of the position, we use the name (keyword) in order to specify the arguments to the function.

What are the advantages of keyword arguments in Python ?

There are some advantages to using keywords arguments using Python, but we're only going to focus on two:

  • It makes the function easier to use since we do not need to worry about the order of the arguments.
  • It allows us to give values to those only parameters that we want, provided that the other parameters have default argument values.

How does it work ?

Keyword arguments are passed to functions after any required positional arguments, but the order in which keyword arguments appear does not matter.

What does that mean ?

Once a function is called, we can mix both positional and keyword arguments. But, keyword arguments must come after the positional arguments. If a positional argument is passed after a keyword argument, it would generate an error.

An example of using keyword argument

def add(a, b = 5 , c = 10):
    print 'a is', a, ', b is', b, 'and c is', c

add(2, 5)
add(15, c = 4)
add(c = 50, a = 20)

Explanation

The function in the example has one parameter without default argument values and two parameters with default argument values.

In the first call, add(2, 5), the parameter a gets the value 2, the parameter b gets the value 5 and c gets the default value of 10.

In the second call, add(15, c = 4), the variable a gets the value of 15, b gets the default value 5 and c gets the value of 4.

In the third call, add(c = 50, a = 20), we use keyword arguments to specify the values. Notice that we are specifying the value for c before a, although a is defined ​before c in the function.

To round up, keyword arguments in Python are used within functions and make a code or program easier to understand.

Thank You for reading!!!