DEV Community


Posted on • Originally published at

Python *args and **kwargs Made Easy

I do not know about you, but every time I saw some function with *args and **kwargs as parameters, I'd get a little scared. I've even "used" them while doing some backend work with Django without understanding a thing. If you're a self-taught developer like me, I know you've been there too.

A few months ago I decided to stop being lazy and started to research it. To my surprise, they were very easy to grasp when playing with the interpreter but not so much when reading about them. I wrote this post trying to explain args and kwargs the way I would have liked someone explained them to me.


The first thing you need to know is that *args and **kwargs lets you pass an undefined number of arguments and keywords when calling a function:

def some_function(*args, **kwargs):

# call some_function with any number of arguments
some_function(arg1, arg2, arg3)

# call some_function with any number of keywords
some_function(key1=arg1, key2=arg2, key3=arg3)

# call both, arguments and keywords
some_function(arg, key1=arg1)

# or none
Enter fullscreen mode Exit fullscreen mode

Second, the words args and kwargs are conventions. This means they are not imposed by the interpreter, but considered good practice among the Python community:

# This function would work just fine
def some_function(*arguments, **keywords):
Enter fullscreen mode Exit fullscreen mode

A note about conventions:

Even if the above function works, don't do it. Conventions are there to help you write readable code for you and anyone that might be interested in your project.

Other conventions include the 4 space indentation, comments, and imports. Reading the PEP 8 -- Style Guide for Python Code is highly recommended.

So, how does Python know that we want our function to accept multiple arguments and/or keywords? Yes, the answers are the * and ** operators.

Now that we have covered the basics, let's work with them 👊.


We now know how to pass multiple arguments using *args as a parameter to our functions, but how do we work with them? It's easy: all the arguments are contained within the args variable as a tuple:

def some_function(*args):
    print(f'Arguments passed: {args} as {type(args)}')

some_function('arg1', 'arg2', 'arg3')
# Arguments passed: ('arg1', 'arg2', 'arg3') as <class 'tuple'>
Enter fullscreen mode Exit fullscreen mode

We can iterate over them:

def some_function(*args):
    for a in args:

some_function('arg1', 'arg2', 'arg3')
# arg1
# arg2
# arg3
Enter fullscreen mode Exit fullscreen mode

Access the elements with an index:

def some_function(*args):

some_function('arg1', 'arg2', 'arg3')  # arg2
Enter fullscreen mode Exit fullscreen mode


def some_function(*args):

some_function('arg1', 'arg2', 'arg3')
# ('arg1', 'arg2')
Enter fullscreen mode Exit fullscreen mode

Whatever you do with a tuple, you can do it with args.


While arguments are stored in the args variable, keywords are within kwargs, but this time as a dictionary where the key is the keyword:

def some_function(**kwargs):
    print(f'keywords: {kwargs} as {type(kwargs)}')

some_function(key1='arg1', key2='arg2', key3='arg3')
# keywords: {'key1': 'arg1', 'key2': 'arg2', 'key3': 'arg3'} as <class 'dict'>
Enter fullscreen mode Exit fullscreen mode

Again, we can do with kwargs the same we would do with any dictionary.

Iterate over:

def some_function(**kwargs):
    for key, value in kwargs.items():
        print(f'{key}: {value}')

some_function(key1='arg1', key2='arg2', key3='arg3')
# key1: arg1
# key2: arg2
# key3: arg3
Enter fullscreen mode Exit fullscreen mode

Use the get() method:

def some_function(key, **kwargs):

some_function('key3', key1='arg1', key2='arg2', key3='arg3')
# arg3
Enter fullscreen mode Exit fullscreen mode

And a lot more =).


*args and **kwargs may seem scary, but the truth is that they are not that difficult to grasp and have the power to grant your functions with flexibility and readability. If you know about tuples and dictionaries, you are ready to go.

Want to play with args and kwargs? This is an online Jupyter Notebook for you to try.

Also, some examples make use of f-strings, a relatively new way to format strings in Python 3.6+. Here you can read more about it.

Any doubt or suggestion? Please leave a comment and have a nice day!

Top comments (3)

kylerconway profile image
Kyle R. Conway

This is a great explainer. Thank you!

wilfredinni profile image

Thank you!

devworkssimone profile image

Thank from a self taught script kiddie😅