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
February 25, 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 ...