WHY KEYWORD ARGUMENTS AND THE STRING FORMAT

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

Why Keyword Arguments?

When calling functions in Python, you’ll often have to choose between using keyword arguments or positional arguments. Keyword arguments can often be used to make function calls more explicit.

Take this code:

def write_gzip_file(output_file, contents):

with GzipFile(None, 'wt', 9, output_file) as gzip_out:

gzip_out.write(contents)

This takes a file object output_file and contents string and writes a gzipped version of the string to the output file.

This code does the same thing but it uses keyword arguments instead of positional arguments:

def write_gzip_file(output_file, contents):

with GzipFile(fileobj=output_file, mode='wt', compresslevel=9) as gzip_out:

gzip_out.write(contents)

Notice that using this keyword argument call style made it more obvious what each of these three arguments represent.

We were also able to leave off an argument here. The first argument that we left off represents a filename and already has a default value of None. We don’t need a filename here because we’re supposed to pass either a file object or a filename to GzipFile, not both.

We’re actually able to leave another argument off though.

Here’s the same code again, but the compress level has been left at its default value of 9 this time:

def write_gzip_file(output_file, contents):

with GzipFile(fileobj=output_file, mode='wt') as gzip_out:

gzip_out.write(contents)

Because we used named arguments, we were able to leave out two arguments and rearrange the remaining 2 arguments in a sensible order (the file object is more important than the “wt” access mode).

STRING FORMAT METHOD

What role does keyword arguments play in a function, like the string format, that accepts any keyword argument you give it?

It works like:

>>> "Hey! This is {name} and today is {event}".format(name="Google", event="Independence Day")

'Hey! This is Google and today is Independence Day'

How can you write such a function?

Python allows functions to capture any keyword arguments provided to them using the ** operator when defining the function:

def format_attributes(**attributes):

"""Return a string of comma-separated key-value pairs."""

return ", ".join(

f"{param}: {value}" for param, value in attributes.items()

)

That ** operator will allow our format_attributes function to accept any number of keyword arguments. The given arguments will be stored in a dictionary called attributes.

Here’s an example use of our function:

>>> format_attributes(name="Google", website="www.google.com", event="Independence Day")

'name: Google, website: www.google.com, event: Independence Day'

So, remember that you can accept any non-specific keyword arguments to the functions you define and pass the keyword arguments to the functions you call by using the ** operator.

Give your objects the names they deserve with Keyword Arguments! Merry programming!

Read Keyword Arguments and Keyword Arguments-2 my previous two blogs! These three blog(3/3) [were published to emphasise the importance of keyword arguments and their applications in Python 3. Hope you can relate to them and use them as part of your functions.

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.