PYTHON 3: KEYWORD ARGUMENTS IN FUNCTIONS

S. Shruti Sharada
Techiepedia
Published in
3 min readAug 9, 2020

Importance of keyword arguments

Most arguments are identified by their position in the function call. The call, random.randint(1, 10) is different from random.randint(10, 1) because random.randint(1, 10) will return a random integer between 1 and 10, 1 as the low end of the range and 10 as the high end, by default . These arguments are called positional arguments. Here, the function call, random.randint(10, 1) causes an error.

However, we also have the keyword arguments, also called ‘named arguments’, identified by the keyword put before them in the function call, rather than their position.

Many programmers are not aware of the various uses of the keyword arguments used in functions. So, this blog exposes the various use-cases of keyword arguments that can be used in functions.

Keyword Arguments on print( ) function

Python has a number of functions that take an unlimited number of positional arguments. These functions sometimes have arguments that can be provided to customize their functionality. Those arguments must be provided as named arguments to distinguish them from the unlimited positional arguments.

The built-in print function accepts the optional sep, end, file, and flush attributes as keyword-only arguments:

>>>>print('My','first','blog’,sep='-')

'My'-'first'-'blog'

>>>> print('comma', 'separated', 'words', sep=', ')

comma, separated, words

>>>>print('Hey!',end='')

>>>>print('You're reading my blog')

'Hey! You're reading my blog'

You can see that using the end keyword, you can print the contents of two print functions on the same line, which is the not the default case with the print function.

Keyword Arguments on user-defined functions

Let’s take this Python function:

from math import sqrt

def quadratic(a, b, c):

x1 = -b / (2*a)

x2 = sqrt(b**2 - 4*a*c) / (2*a))

return (x1 + x2), (x1 - x2)

We can pass our arguments as positional arguments like this:

>>> quadratic(31, 93, 62)

(-1.0, -2.0)

Or we can pass our arguments as keyword arguments like this:

>>> quadratic(a=31, b=93, c=62)

(-1.0, -2.0)

The order of these arguments matters when they’re passed positionally:

>>> quadratic(31, 93, 62)

(-1.0, -2.0)

>>> quadratic(62, 93, 31)

(-0.5, -1.0)

You can see that changing the order, changes the output.

But it doesn’t matter, when they’re passed by their name:

>>> quadratic(a=31, b=93, c=62)

(-1.0, -2.0)

>>> quadratic(c=62, b=93, a=31)

(-1.0, -2.0)

When we use keyword/named arguments, it’s the name that matters, not the position:

>>> quadratic(a=31, b=93, c=62)

(-1.0, -2.0)

>>> quadratic(c=31, b=93, a=62)

(-0.5, -1.0)

If we ask for help on our function Python will tell us our three arguments by name:

>>> help(quadratic)

Help on function quadratic in module __main__:

quadratic(a, b, c)

Note that functions can be called with a mix of positional and keyword arguments:

>>> quadratic(31, 93, c=62)

(-1.0, -2.0)

Why use keyword arguments?

  1. We can often leave out arguments that have default values.
  2. We can rearrange arguments in a way that makes them most readable.
  3. We call arguments by their names to make it more clear what they represent.

Where you see keyword arguments

You’ll likely see keyword arguments quite a bit in Python.

The itertools.zip_longest function also accepts an optional fillvalue attribute (which defaults to None) exclusively as a keyword argument:

>>> from itertools import zip_longest

>>> list(zip_longest([1, 2], [7, 8, 9], [4, 5], fillvalue=0))

[(1, 7, 4), (2, 8, 5), (0, 9, 0)]

In fact, some functions in Python force arguments to be named even when they could have been unambiguously specified positionally.

Python 3’s sorted function requires that all arguments after the provided iterable ones to be specified as keyword arguments:

>>> sorted([4, 1, 8, 2, 7], None, True)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

TypeError: must use keyword argument for key function

>>> sorted([4, 1, 8, 2, 7], reverse=True)

[8, 7, 4, 2, 1]

Keyword arguments come up quite a bit in Python’s built-in functions, like we saw in the print( ) function, as well as in the standard library and third party libraries.

As for arguments, just the position often doesn’t convey as much meaning as its name. So when calling functions, consider naming arguments that you pass in it, if it makes their meaning clearer.

Do Read My Next 2 Blogs related to this as well(1/3).Coming Soon!

References

  1. https://automatetheboringstuff.com/2e/chapter3/
  2. https://treyhunner.com/2018/04/keyword-arguments-in-python/
  3. https://www.python.org/dev/peps/pep-0468/

--

--

S. Shruti Sharada
Techiepedia

Hey! I joined Medium 3 years ago, as a final year student, and I write blogs on anything that interests me. I also write blogs for the publication, Techiepedia.