What Are Args and Kwargs in Python?
Make a function accept any number of arguments by using *args and **kwargs
In Python, both *args
and **kwargs
mean that a function can accept any number of arguments.
*args
refer to regular arguments (e.g.myFun(1,2,3)
).**kwargs
refer to keyword arguments (e.g.myFun(name="Charlie")
).
Today, you will learn how you can implement functions that can accept any number of arguments/keyword arguments.
Args in Python
Let’s create a function called mySum
that accepts any number of arguments and sums them up:
def mySum(*args):
s = 0
for num in args:
s += num
return s
mySum(1,2) # returns 3
mySum(1,2,2,3,4) # returns 12
Notice how *args
are looped just like a list of numbers. This is because *args
is a list of number arguments. So you could actually pass a list to mySum
as well:
numbers = [1,2,3]
mySum(*numbers) # returns 6
It is really that simple. Next, let’s take a look at **kwargs
.
Kwargs in Python
You can use **kwargs
to pass any number of keyword arguments to a function. A keyword argument is just an argument that has a name (e.g. myFun(name="Jack")
).
As an example, let’s create a function that prints some items:
def read(**kwargs):
for key, value in kwargs.items():
print("{}: {}".format(key, value))
read(shoes="Adidas", shirt="H&M")
read(socks="Nike")
Output:
shoes: Adidas
shirt: H&M
socks: Nike
Notice how **kwargs
is looped through just like a dictionary. This is because **kwargs
actually is a dictionary. This means you could pass a dictionary of items to the read
function:
clothes = {"shoes": "Adidas", "shirt": "H&M"}
read(**clothes)
Output:
shoes: Adidas
shirt: H&M
Now you know how to work with *args
and **kwargs
. Next, let’s use both in the same function.
Use Both Args and Kwargs
Finally, you can easily mix things up and use args and *kwargs in the same function. For example:
def read_all(*args, **kwargs):
for value in args:
print(value)
for key, value in kwargs.items():
print("{}: {}".format(key, value))
# prints all the numbers and clothes passed to read_all function:
read_all(1,2,3,shirt="H&M",socks="Nike")
Naming Convention
Now that you understand *args
and **kwargs
, it is a good time to point out that there is nothing special about the words args
and kwargs
. The naming is actually up to you:
*args
could just as well be named *numbers
.
**kwargs
could just as well be named **items
.
All you need is to make sure to use *
when dealing with regular arguments and **
when dealing with keyword arguments.
Conclusion
In Python, you can pass any number of arguments to a function by using *args
or **kwargs
.
- Use
*args
when dealing with regular arguments. DefiningmyFun(*args)
makes it possible to call e.g.myFun(1,2)
ormyFun(10,1,38,7,10,2)
. - Use
**kwargs
when dealing with keyword arguments. DefiningmyFun(**kwargs)
makes it possible to call for examplemyFun(name="Jim")
ormyFun(socks="Nike", shirt="H&M")
.
Notice that *args
and **kwargs
can actually be named whatever you like. Remember to use *
with regular arguments and **
with keyword arguments.
Thanks for reading. I hope you found this useful. Happy coding!