Skip to content

*args and **kwargs in Python, with Examples

One of the things we all overlook when starting to learn Python, and one of the most puzzling, is functions (or methods) that take *args and **kwargs as parameters.

Ricardo Moya

When you start with a programming language, like Python, we usually learn it hands-on (by programming), skipping over the theory of the language itself. One of the things we all overlook when starting to learn Python, and one of the most puzzling, is functions (or methods) that take *args and **kwargs as parameters. To understand how they work, you need to be clear on the concepts of List and Dictionary in Python.

Note

*args: is a list of arguments, like positional arguments.

**kwargs: is a dictionary whose keys become parameters and whose values become the arguments for those parameters.

Note that the syntax is * and **, while the names args and kwargs are just a convention and not a strict requirement; that is, if we pass a function *my_list or **my_dictionary as an argument, it will have the same effect.

*args is used when you are not sure how many arguments will need to be passed to the function; that is, it lets you pass an arbitrary number of arguments. Let’s look at an example:

def read_list_args(*args):
    for count, arg in enumerate(args):
        print '%d - %s' % (count, arg)

print 'First'
read_list_args('Ricardo', 'jarroba.com')

print 'Second'
read_list_args('Ricardo', 23, 'Ramon', [1, 2, 3], 'jarroba.com')

As we can see, we call the function with a different number of arguments, and the output of the function is the following:

First
0 - Ricardo
1 - jarroba.com
Second
0 - Ricardo
1 - 23
2 - Ramon
3 - [1, 2, 3]
4 - jarroba.com

Similarly, when you don’t know how many arguments a function is going to receive, **kwargs accepts arguments for parameters with names that haven’t been defined beforehand. Let’s look at an example:

def read_dict_args(**kwargs):
    for key, value in kwargs.items():
        print '%s - %s' % (key, value)

print 'First'
read_dict_args(name1='Ricardo', name2='Ramon', web='jarroba.com')

print 'Second'
read_dict_args(Team='FC Barcelona', player='Iniesta', demarcation='Right winger', number=8)

The output of this code is the following:

First
web - jarroba.com
name2 - Ramon
name1 - Ricardo
Second
player - Iniesta
number - 8
demarcation - Right winger
Team - FC Barcelona

Both * and ** can be used together and alongside other parameters defined in the function, but in this case order does matter. First come the parameters defined for the function, second *args, and third **kwargs:

def do_something(a, b, *args, **kwargs):

Note

What is a parameter and what is an argument?

In this post the words parameter and argument have been used several times. It’s important to know the difference between them, since it’s quite easy to mix them up and many people think they are synonyms in the context of programming:

  • Parameter: Specified in the definition of the function or method.
  • Argument: The value assigned to the parameters when the function is called.
def something(parameter1, parameter2):
...

something(parameter1=argument1, parameter2=argument2)

References:

“*args and **kwargs in Python, with Examples” by Ricardo Moya at jarroba.com, licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0. Registered at Safe Creative.

Ricardo Moya

I work in Machine Learning and Big Data. I like learning new technologies and explaining them as clearly as I can on this site.

You may also like

Comments

Leave a comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.