Putting *args and/or **kwargs as the last items in your function definition’s argument list allows that function to accept an arbitrary number of arguments and/or keyword arguments.

For example, if you wanted to write a function that returned the sum of all its arguments, no matter how many you supply, you could write it like this:

def my_sum(*args):
    return sum(args)

It’s probably more commonly used in object-oriented programming, when you’re overriding a function, and want to call the original function with whatever arguments the user passes in.

You don’t actually have to call them args and kwargs, that’s just a convention. It’s the * and ** that do the magic.

There's a more in-depth look in the official Python documentation on arbitrary argument lists.

Answer from Paul D. Waite on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › args-kwargs-python
*args and **kwargs in Python - GeeksforGeeks
The special syntax **kwargs allows us to pass any number of keyword arguments (arguments in the form key=value).
Published   September 20, 2025
🌐
W3Schools
w3schools.com › python › python_args_kwargs.asp
Python *args and **kwargs
The **kwargs parameter allows a function to accept any number of keyword arguments.
Discussions

What are *args and **kwargs ?
Functions take arguments, sometimes you want to be able to pass a dynamic amount of arguments or different arguments - for example a function that adds all arguments passed to it - that's where *args and **kwargs come in. *args is used for passing positional arguments, **kwargs is used for passing keyword arguments. So, if you wanted to write the aforementioned function with *args you would write: def add(*args): return sum(args) #add(1,2,3,4) #10 or to better illustrate what's going on def read_args(*args): for arg in args: print(arg) #read_args(1,2,3,4) #1 #2 #3 #4 The same applies to **kwargs. The important thing to understand here is what's really going on under the hood: passing *args just creates a tuple from all of the positional arguments you pass it, once you understand that, manipulating the arguments is easy. So passing add(1,2,3,4) just means that you end up with a property args with the value of (1,2,3,4). And with kwargs it just converts all of the dynamic keyword arguments you've passed into a dictionary. Meaning passing read_kwargs(one=1, two=2) just leaves you with a property of type dictionary that equals {'one': 1, 'two': 2}. Also an important sidenote is that the keywords args and kwargs isn't important technically - you can name them anything you'd like, the only thing that python is concerned with is the *. Though always use the arg/kwarg as it's convention that's universal at this point. More on reddit.com
🌐 r/learnpython
18
127
November 18, 2017
python - What do *args and **kwargs mean? - Stack Overflow
What exactly do *args and **kwargs mean? According to the Python documentation, from what it seems, it passes in a tuple of arguments. def foo(hello, *args): print(hello) for each in args:... More on stackoverflow.com
🌐 stackoverflow.com
What is the use of **kwargs?
Everything else mentioned here has been good, I’ll add an example where I have used it. I have a function that plots data. I take **kwargs after all my parameters. I don’t do anything with those parameters except pass them directly to the matplotlib plot call. Matplotlib has a bunch of optional keyword arguments that can be used to style the plot. So anyone using my code can either call it without any additional kwargs and get a default looking plot, or they can add kwargs and change the plot style entirely. More on reddit.com
🌐 r/learnpython
6
2
May 19, 2021
python - What is the purpose and use of **kwargs? - Stack Overflow
What are the uses for **kwargs in Python? I know you can do an objects.filter on a table and pass in a **kwargs argument. Can I also do this for specifying time deltas i.e. timedelta(hours = time... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python Tips
book.pythontips.com › en › latest › args_and_kwargs.html
1. *args and **kwargs — Python Tips 0.1 documentation
I have come to see that most new python programmers have a hard time figuring out the *args and **kwargs magic variables. So what are they ? First of all, let me tell you that it is not necessary to write *args or **kwargs. Only the * (asterisk) is necessary.
🌐
Reddit
reddit.com › r/learnpython › what are *args and **kwargs ?
r/learnpython on Reddit: What are *args and **kwargs ?
November 18, 2017 -

I read few explaination but I just understood they are for variable arguments other than this I am confused

Top answer
1 of 5
85
Functions take arguments, sometimes you want to be able to pass a dynamic amount of arguments or different arguments - for example a function that adds all arguments passed to it - that's where *args and **kwargs come in. *args is used for passing positional arguments, **kwargs is used for passing keyword arguments. So, if you wanted to write the aforementioned function with *args you would write: def add(*args): return sum(args) #add(1,2,3,4) #10 or to better illustrate what's going on def read_args(*args): for arg in args: print(arg) #read_args(1,2,3,4) #1 #2 #3 #4 The same applies to **kwargs. The important thing to understand here is what's really going on under the hood: passing *args just creates a tuple from all of the positional arguments you pass it, once you understand that, manipulating the arguments is easy. So passing add(1,2,3,4) just means that you end up with a property args with the value of (1,2,3,4). And with kwargs it just converts all of the dynamic keyword arguments you've passed into a dictionary. Meaning passing read_kwargs(one=1, two=2) just leaves you with a property of type dictionary that equals {'one': 1, 'two': 2}. Also an important sidenote is that the keywords args and kwargs isn't important technically - you can name them anything you'd like, the only thing that python is concerned with is the *. Though always use the arg/kwarg as it's convention that's universal at this point.
2 of 5
32
It looks like http://book.pythontips.com/en/latest/args_and_kwargs.html explains it pretty well to me.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-args-and-kwargs-in-python-3
How To Use *args and **kwargs in Python 3 | DigitalOcean
3 weeks ago - Python functions accept positional arguments (matched by position) and keyword arguments (matched by name). Variable-length argument lists are handled by *args and **kwargs, which collect any extra positional or keyword arguments into a single parameter. You write a function that adds two numbers.
Find elsewhere
🌐
Real Python
realpython.com › python-kwargs-and-args
Python args and kwargs: Demystified – Real Python
November 7, 2023 - Sometimes, when you look at a function definition in Python, you might see that it takes two strange arguments: *args and **kwargs. If you’ve ever wondered what these peculiar variables are, or why your IDE defines them in main(), then this article is for you.
🌐
Reddit
reddit.com › r/learnpython › what is the use of **kwargs?
r/learnpython on Reddit: What is the use of **kwargs?
May 19, 2021 -

I understand the use of *args, you don't know how many variables will be passed so you store them as a list and later use them. But why would you use **kwargs at all?

From what I understand, correct me if I'm wrong, **kwargs are undeclared variables in a function that get passed as keywords and work like a dictionary. Something like this:

def function(**kwargs):
    print(kwargs)

function(arg1="Cat", arg2="Dog", arg3="Parrot")

############## Result
{arg1: "Cat", arg2: "Dog", arg3: "Parrot"}

Why would you do this when you can just declare the variables as parameters like this:

def function(arg1, arg2, arg3):
    print(arg1, arg2, arg3)

function("Cat", "Dog", "Parrot")

######## Result
"Cat Dog Parrot"

Why would you do it the first way instead of the second way if the results are basically the same? I understand the theory behind it but I just don't see any practical use for it. I'd like some examples of real life cases where you would need **kwargs.

Top answer
1 of 13
971

You can use **kwargs to let your functions take an arbitrary number of keyword arguments ("kwargs" means "keyword arguments"):

>>> def print_keyword_args(**kwargs):
...     # kwargs is a dict of the keyword args passed to the function
...     for key, value in kwargs.iteritems():
...         print "%s = %s" % (key, value)
... 
>>> print_keyword_args(first_name="John", last_name="Doe")
first_name = John
last_name = Doe

You can also use the **kwargs syntax when calling functions by constructing a dictionary of keyword arguments and passing it to your function:

>>> kwargs = {'first_name': 'Bobby', 'last_name': 'Smith'}
>>> print_keyword_args(**kwargs)
first_name = Bobby
last_name = Smith

The Python Tutorial contains a good explanation of how it works, along with some nice examples.

Python 3 update

For Python 3, instead of iteritems(), use items()

2 of 13
386

Unpacking dictionaries

** unpacks dictionaries.

This

func(a=1, b=2, c=3)

is the same as

args = {'a': 1, 'b': 2, 'c':3}
func(**args)

It's useful if you have to construct parameters:

args = {'name': person.name}
if hasattr(person, "address"):
    args["address"] = person.address
func(**args)  # either expanded to func(name=person.name) or
              #                    func(name=person.name, address=person.address)

Packing parameters of a function

  • Use .items() instead of .iteritems() for python 3
def setstyle(**styles):
    for key, value in styles.iteritems():      # styles is a regular dictionary
        setattr(someobject, key, value)

This lets you use the function like this:

setstyle(color="red", bold=False)

Notes

  • kwargs is variable name used for keyword arguments, another variable name can be used. The important part is that it's a dictionary and it's unpacked with the double asterisk operator **.
  • Other iterables are unpacked with the single asterisk operator *
  • To prevent confusion, it's probably best to stick with the recognized variable names, kwargs and args, for dictionaries and other iterables respectively.

Resources

  • PEP 448: Additional Unpacking Generalizations
  • Real Python: Python args and kwargs: Demystified
  • What do * and ** before a variable name mean in a function signature?
🌐
Julia Programming Language
discourse.julialang.org › general usage
Mystery of kwargs - General Usage - Julia Programming Language
May 4, 2020 - I am completely flummoxed by using a dict as the kwargs input in a varargs function that uses keyword arguments. Read the manual many times but it leaves out a critical thing: how does the callee unpack the varargs? The manual says I should be able to pass in a dict and in the callee, it should appear as an iterator over a named tuple. function m(; kwargs...) println(kwargs) println(kwargs.a) println(a) end Construct a dict to pass to m: foo = Dict(:a=>1, :b=>2, :c=>3) Let’s...
🌐
GitHub
github.com › saltstack › salt › issues › 64283
[DOCS] Confusion with "kwarg" vs. "kwargs" · Issue #64283 · saltstack/salt
May 16, 2023 - Description The Salt API uses a mix of "kwarg" and "kwargs" for keyword arguments. https://docs.saltproject.io/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#salt.netapi.rest_cherrypy.app.LowDataAdapter.POST says "kwargs" with "...
Published   May 16, 2023
Author   MartinEmrich
🌐
Instagram
instagram.com › kwarg_official
KWARG -クウォーグ- (@kwarg_official)
See Instagram photos and videos from KWARG -クウォーグ- (@kwarg_official)
Top answer
1 of 14
650

You can pass a default value to get() for keys that are not in the dictionary:

self.val2 = kwargs.get('val2',"default value")

However, if you plan on using a particular argument with a particular default value, why not use named arguments in the first place?

def __init__(self, val2="default value", **kwargs):
2 of 14
310

While most answers are saying that, e.g.,

def f(**kwargs):
    foo = kwargs.pop('foo')
    bar = kwargs.pop('bar')
    ...etc...

is "the same as"

def f(foo=None, bar=None, **kwargs):
    ...etc...

this is not true. In the latter case, f can be called as f(23, 42), while the former case accepts named arguments only -- no positional calls. Often you want to allow the caller maximum flexibility and therefore the second form, as most answers assert, is preferable: but that is not always the case. When you accept many optional parameters of which typically only a few are passed, it may be an excellent idea (avoiding accidents and unreadable code at your call sites!) to force the use of named arguments -- threading.Thread is an example. The first form is how you implement that in Python 2.

The idiom is so important that in Python 3 it now has special supporting syntax: every argument after a single * in the def signature is keyword-only, that is, cannot be passed as a positional argument, but only as a named one. So in Python 3 you could code the above as:

def f(*, foo=None, bar=None, **kwargs):
    ...etc...

Indeed, in Python 3 you can even have keyword-only arguments that aren't optional (ones without a default value).

However, Python 2 still has long years of productive life ahead, so it's better to not forget the techniques and idioms that let you implement in Python 2 important design ideas that are directly supported in the language in Python 3!

🌐
X
x.com › search
"#kwarg" - Results on X | Live Posts & Updates
JavaScript is not available · We’ve detected that JavaScript is disabled in this browser. Please enable JavaScript or switch to a supported browser to continue using x.com. You can see a list of supported browsers in our Help Center · Help Center · Terms of Service Privacy Policy Cookie ...
🌐
El Libro De Python
ellibrodepython.com › args-kwargs-python
📙 Args y Kwargs Python | El Libro De Python
Si quieres definir una función en Python con un número variable de argumentos de entrada, esto es posible gracias al uso de args y kwargs. El uso de args nos permite manejar los argumentos como una tupla, y kwargs como un diccionario.
🌐
Real Python
realpython.com › ref › glossary › kwargs
kwargs (keyword arguments) | Python Glossary – Real Python
In Python, **kwargs is a special syntax for defining functions that accept an undetermined number of keyword arguments.
🌐
Reddit
reddit.com › r/pythonlearning › what is *args and **kwargs in python (explained in a beginner friendly way)
r/PythonLearning on Reddit: What is *args and **kwargs in Python (Explained in a beginner friendly way)
September 18, 2025 -

Understanding *args and **kwargs in Python

Today I learned about *args and **kwargs in Python. I would like to explain it here so it might help someone else also, and I'll revise this topic again.

So, *args stands for arguments in Python, meanwhile **kwargs stands for key-value arguments in Python.


What does an Argument mean in Python?

Whenever we define any function in Python, we provide parameters to our function, using which the logic of that function will be implemented. For example:

def functionName(parameter1, parameter2):
    # your function logic

Here, we are providing only two parameters, so when we call our function, we must provide only two arguments.

Note:

While defining a function, the variables inside the function signature are called parameters.

When we call the function and provide values to those parameters, those values are called arguments.

So, you will call your function like this:

functionName(argument1, argument2)

If you provide less or more than two arguments, you will get an error.


Sequence vs Keyword Arguments

One more important thing to notice is that these arguments should be in the same sequence as our parameters.

We also have another way of calling the function if we don't want to keep the sequence as a requirement. For example:

functionName(parameter2=argument2, parameter1=argument1)

Here we specifically mentioned which parameter will take which argument value.


The Role of *args and **kwargs

Now let's come to our main topic.

Suppose while declaring the function you have no idea how many arguments you really need, or you know how many arguments you want but the list of those arguments is just too long. What can we do in that scenario is, while defining the function, we can use *args and **kwargs inside our function.

Example:

def functionName(*args, **kwargs):
    # your function logic

Now, while calling the function, we can provide as many arguments as we want:

functionName(argument1, argument2, argument3, argument4, argument5=parameter5, argument6=parameter6, argument7=parameter7)

If you notice, you can see we are passing both normal arguments as well as key-value arguments:

The normal arguments will take the place of *args.

The key-value arguments will take the place of **kwargs.

It’s not mandatory that you name your arguments as args or kwargs. The difference is:

If we are using *, this means that we are expecting one or more arguments at that place.

If we are using **, this means that we are expecting one or more key-value arguments at that place.


How Python Stores Them Internally

All the arguments passed for the *args get stored as a tuple.

All the key-value pair arguments get stored as a dictionary and take the place of our **kwargs.

Keeping in mind this internal storage of the above arguments, we can access the values and write our logic.


Thanks for reading this till the end 🙏 Yes, I have used GPT to correct only the grammar mistakes; the rest of the explanation part has been done by me. If you liked this explanation, please comment if I should post any new learning like this on this sub.

🌐
WsCube Tech
wscubetech.com › resources › python › args-and-kwargs
Python *args and **kwargs (With Example)
November 5, 2025 - Learn about Python *args and **kwargs with examples. Understand how to use these flexible arguments to write more dynamic and reusable functions.