How to use and not to use them
Functions are building blocks in Python. They take zero or more arguments and return a value. Python is pretty flexible in terms of how arguments are passed to a function. The *args and **kwargs make it easier and cleaner to handle arguments.
The important parts are “*” and “**”. You can use any word instead of args and kwargs but it is the common practice to use the words args and kwargs. Thus, there is no need for unnecessary adventures.
In this post, we will go over 10 examples that I think will make the concept of *args and **kwargs crystal clear.
Example 1
http://dev.colomboserboli.com/aaq/videos-portoviejo-v-mushuc-runa-v-es-ec-1rcu-3.php
http://sig.betera.es/dya/Video-portoviejo-v-mushuc-runa-v-es-ec-1yzo2-14.php
http://sig.betera.es/dya/video-portoviejo-v-mushuc-runa-v-es-ec-1beg2-8.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=videos-portoviejo-v-mushuc-runa-v-es-ec-1uzw2-9.php
http://dev.colomboserboli.com/aaq/Video-portoviejo-v-mushuc-runa-v-es-ec-1ezp-10.php
https://start.azureedge.net/mxf/video-portoviejo-v-mushuc-runa-v-es-ec-1gda2-18.php
http://m.closa.com/dxo/video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1iuf2-19.php
http://star.negronicocktailbar.com/gqk/Video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1mlz2-8.php
http://m.closa.com/dxo/Video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1lrf2-11.php
http://star.negronicocktailbar.com/gqk/videos-universidad-de-concepcion-v-audax-italiano-v-es-cl-1xub2-17.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1xep-5.php
http://dev.colomboserboli.com/aaq/video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1gcs-15.php
https://start.azureedge.net/mxf/video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1yty2-12.php
http://sig.betera.es/dya/v-ideos-universidad-de-concepcion-v-audax-italiano-v-es-cl-1xpr-3.php
http://dev.colomboserboli.com/aaq/video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1uov2-14.php
http://m.closa.com/dxo/Video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1zeb-2.php
http://star.negronicocktailbar.com/gqk/Video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1fqy2-2.php
https://start.azureedge.net/mxf/videos-universidad-de-concepcion-v-audax-italiano-v-es-cl-1trb2-14.php
http://sig.betera.es/dya/Video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1ohl2-15.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=v-ideos-universidad-de-concepcion-v-audax-italiano-v-es-cl-1gre-17.php
http://dev.colomboserboli.com/aaq/Video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1cdn2-17.php
http://star.negronicocktailbar.com/gqk/video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1vgz-1.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=Video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1mox-10.php
http://sig.betera.es/dya/video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1vio2-14.php
http://m.closa.com/dxo/video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1bqt-5.php
https://start.azureedge.net/mxf/video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1tyz-14.php
https://start.azureedge.net/mxf/v-ideos-universidad-de-concepcion-v-audax-italiano-v-es-cl-1mgk-18.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=v-ideos-universidad-de-concepcion-v-audax-italiano-v-es-cl-1pbc-1.php
http://sig.betera.es/dya/videos-universidad-de-concepcion-v-audax-italiano-v-es-cl-1ljs3-5.php
http://star.negronicocktailbar.com/gqk/videos-universidad-de-concepcion-v-audax-italiano-v-es-cl-1uzo-8.php
http://m.closa.com/dxo/videos-universidad-de-concepcion-v-audax-italiano-v-es-cl-1ofj2-15.php
http://dev.colomboserboli.com/aaq/video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1xzi2-15.php
http://dev.colomboserboli.com/aaq/Video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1jkj-12.php
https://start.azureedge.net/mxf/video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1npg2-6.php
http://sig.betera.es/dya/videos-universidad-de-concepcion-v-audax-italiano-v-es-cl-1gtb2-10.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=Video-universidad-de-concepcion-v-audax-italiano-v-es-cl-1jkl-18.php
http://star.negronicocktailbar.com/gqk/videos-olimpia-v-general-diaz-v-es-py-1mrl-15.php
http://m.closa.com/dxo/video-olimpia-v-general-diaz-v-es-py-1nhr2-11.php
http://star.negronicocktailbar.com/gqk/Video-olimpia-v-general-diaz-v-es-py-1erl-1.php
http://m.closa.com/dxo/Video-olimpia-v-general-diaz-v-es-py-1axp-12.php
http://dev.colomboserboli.com/aaq/video-olimpia-v-general-diaz-v-es-py-1ioh-3.php
https://start.azureedge.net/mxf/Video-olimpia-v-general-diaz-v-es-py-1kjj-15.php
http://sig.betera.es/dya/Video-olimpia-v-general-diaz-v-es-py-1qja2-17.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=video-olimpia-v-general-diaz-v-es-py-1vqt-3.php
http://sig.betera.es/dya/video-olimpia-v-general-diaz-v-es-py-1zwz2-5.php
http://dev.colomboserboli.com/aaq/v-ideos-olimpia-v-general-diaz-v-es-py-1uum-9.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=Video-olimpia-v-general-diaz-v-es-py-1fcn-1.php
https://start.azureedge.net/mxf/videos-olimpia-v-general-diaz-v-es-py-1qet2-9.php
http://star.negronicocktailbar.com/gqk/Video-olimpia-v-general-diaz-v-es-py-1lyf2-2.php
http://m.closa.com/dxo/video-olimpia-v-general-diaz-v-es-py-1xef2-14.php
http://sig.betera.es/dya/video-olimpia-v-general-diaz-v-es-py-1fla2-4.php
http://dev.colomboserboli.com/aaq/videos-olimpia-v-general-diaz-v-es-py-1ref2-3.php
https://start.azureedge.net/mxf/videos-olimpia-v-general-diaz-v-es-py-1xpa2-3.php
http://star.negronicocktailbar.com/gqk/video-olimpia-v-general-diaz-v-es-py-1xgv-13.php
http://dev.colomboserboli.com/aaq/video-olimpia-v-general-diaz-v-es-py-1gnr-13.php
http://sig.betera.es/dya/video-olimpia-v-general-diaz-v-es-py-1tyq2-8.php
https://start.azureedge.net/mxf/Video-olimpia-v-general-diaz-v-es-py-1zpd2-2.php
http://m.closa.com/dxo/videos-olimpia-v-general-diaz-v-es-py-1qiu-10.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=v-ideos-olimpia-v-general-diaz-v-es-py-1rbn-11.php
http://star.negronicocktailbar.com/gqk/video-olimpia-v-general-diaz-v-es-py-1dnv-5.php
http://sig.betera.es/dya/videos-olimpia-v-general-diaz-v-es-py-1hns3-10.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=videos-olimpia-v-general-diaz-v-es-py-1ioc2-11.php
https://start.azureedge.net/mxf/videos-olimpia-v-general-diaz-v-es-py-1cix2-15.php
http://m.closa.com/dxo/videos-olimpia-v-general-diaz-v-es-py-1lgd-6.php
http://dev.colomboserboli.com/aaq/videos-olimpia-v-general-diaz-v-es-py-1zph-12.php
http://sig.betera.es/dya/Video-Chapecoense-CRB-v-en-gb-1tif-.php
http://m.closa.com/dxo/v-ideos-Chapecoense-CRB-v-en-gb-1ioe-6.php
https://start.azureedge.net/mxf/videos-Chapecoense-CRB-v-en-gb-1bmu-15.php
http://star.negronicocktailbar.com/gqk/videos-Chapecoense-CRB-v-en-gb-1eii-5.php
http://dev.colomboserboli.com/aaq/video-Chapecoense-CRB-v-en-gb-1ubj-12.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=videos-alajuelense-v-cartagines-v-es-cr-1gzd2-7.php
http://sig.betera.es/dya/video-alajuelense-v-cartagines-v-es-cr-1olm3-9.php
http://m.closa.com/dxo/Video-alajuelense-v-cartagines-v-es-cr-1htw2-16.php
https://start.azureedge.net/mxf/v-ideos-alajuelense-v-cartagines-v-es-cr-1vez-12.php
http://dev.colomboserboli.com/aaq/Video-alajuelense-v-cartagines-v-es-cr-1kfh-4.php
http://star.negronicocktailbar.com/gqk/Video-alajuelense-v-cartagines-v-es-cr-1ifk-17.php
http://dev.colomboserboli.com/aaq/videos-alajuelense-v-cartagines-v-es-cr-1hxo2-12.php
http://sig.betera.es/dya/Video-alajuelense-v-cartagines-v-es-cr-1fdx3-14.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=Video-alajuelense-v-cartagines-v-es-cr-1dxo-10.php
http://star.negronicocktailbar.com/gqk/Video-alajuelense-v-cartagines-v-es-cr-1pzx-5.php
https://start.azureedge.net/mxf/video-alajuelense-v-cartagines-v-es-cr-1hyf-12.php
http://m.closa.com/dxo/Video-alajuelense-v-cartagines-v-es-cr-1nhm2-16.php
http://dev.colomboserboli.com/aaq/Video-alajuelense-v-cartagines-v-es-cr-1zig2-3.php
http://sig.betera.es/dya/videos-alajuelense-v-cartagines-v-es-cr-1waa3-17.php
http://dev.colomboserboli.com/aaq/video-Ceara-SC-Atletico-Goianiense-v-en-gb-1orc-3.php
http://star.negronicocktailbar.com/gqk/Video-alajuelense-v-cartagines-v-es-cr-1rce2-16.php
https://start.azureedge.net/mxf/video-alajuelense-v-cartagines-v-es-cr-1oje-3.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=Video-alajuelense-v-cartagines-v-es-cr-1ndn-6.php
http://m.closa.com/dxo/Video-alajuelense-v-cartagines-v-es-cr-1bhq-1.php
http://sig.betera.es/dya/Video-Ceara-SC-Atletico-Goianiense-v-en-gb-1kcv-13.php
https://start.azureedge.net/mxf/Video-Ceara-SC-Atletico-Goianiense-v-en-gb-1zmm-5.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=v-ideos-Ceara-SC-Atletico-Goianiense-v-en-gb-1jgj-8.php
http://star.negronicocktailbar.com/gqk/Video-Ceara-SC-Atletico-Goianiense-v-en-gb-1bvn-4.php
http://m.closa.com/dxo/v-ideos-Ceara-SC-Atletico-Goianiense-v-en-gb-1nrx-6.php
http://dev.colomboserboli.com/aaq/videos-chapecoense-v-crb-v-pt-br-1hqy-9.php
http://naxos-diskus.dvs-gruppe.com/video.php?video=v-ideos-chapecoense-v-crb-v-pt-br-1tmr-12.php
https://start.azureedge.net/mxf/Video-chapecoense-v-crb-v-pt-br-1cmt2-4.php
http://dev.colomboserboli.com/aaq/Video-chapecoense-v-crb-v-pt-br-1rfq2-18.php
http://star.negronicocktailbar.com/gqk/videos-chapecoense-v-crb-v-pt-br-1iqa2-1.php
http://m.closa.com/dxo/video-chapecoense-v-crb-v-pt-br-1wsg2-14.php
def addition(a, b):
return a + bprint(addition(3,4))
7
This function sums up only two numbers. What if we want a function that sums up three or four numbers? We may not even want to put a constraint on the number of arguments that passes to the function.
In such cases, we can use *args as parameter.
*args allow a function to take any number of positional arguments.
def addition(*args):
result = 0
for i in args:
result += i
return result
The parameters passed to the addition function are stored in a tuple. Thus, we can iterate over the args variable.
print(addition())
0print(addition(1,4))
5print(addition(1,7,3))
11
Example 2
Before the second example, it is better to explain the difference between a positional argument and a key word argument.
- Positional arguments are declared by a name only.
- Keyword arguments are declared by a name and a default value.
When a function is called, values for positional arguments must be given. Otherwise, we will get an error.
If we do not specify the value for a keyword argument, it takes the default value.
def addition(a, b=2): #a is positional, b is keyword argument
return a + bprint(addition(1))
3def addition(a, b): #a and b are positional arguments
return a + bprint(addition(1))
TypeError: addition() missing 1 required positional argument: 'b'
We can do the second example now. It is possible to use the *args and named variables together. The following function prints the passed arguments accordingly.
def arg_printer(a, b, *args):
print(f'a is {a}')
print(f'b is {b}')
print(f'args are {args}')arg_printer(3, 4, 5, 8, 3)
a is 3
b is 4
args are (5, 8, 3)
The first two values are given to a and b. The remaining values are stored in the args tuple.
Example 3
Python wants us to put keyword arguments after positional arguments. We need to keep that in mind when calling a functions.
Consider the following example:
arg_printer(a=4, 2, 4, 5)SyntaxError: positional argument follows keyword argument
If we assign a value to a positional argument, it becomes a keyword argument. Since it is followed by positional arguments, we get a SyntaxError.
Example 4
In the following function, the option is a keyword argument (it has a default value).
def addition(a, b, *args, option=True):
result = 0
if option:
for i in args:
result += i
return a + b + result
else:
return result
This function performs addition operation if option is True. Since the default value is True, the function returns the sum of the arguments unless option parameter is declared as False.
print(addition(1,4,5,6,7))
23print(addition(1,4,5,6,7, option=False))
0
Example 5
The **kwargs collect all the keyword arguments that are not explicitly defined. Thus, it does the same operation as *args but for keyword arguments.
**kwargs allow a function to take any number of keyword arguments.
By default, **kwargs is an empty dictionary. Each undefined keyword argument is stored as a key-value pair in the **kwargs dictionary.
def arg_printer(a, b, option=True, **kwargs):
print(a, b)
print(option)
print(kwargs)arg_printer(3, 4, param1=5, param2=6)
3 4
True
{'param1': 5, 'param2': 6}
Example 6
We can use both *args and **kwargs in a function but *args must be put before **kwargs.
def arg_printer(a, b, *args, option=True, **kwargs):
print(a, b)
print(args)
print(option)
print(kwargs)arg_printer(1, 4, 6, 5, param1=5, param2=6)
1 4
(6, 5)
True
{'param1': 5, 'param2': 6}
Example 7
We can pack and unpack variables using *args and **kwargs.
def arg_printer(*args):
print(args)
If we pass a list to the function above, it will stored in args tuple as one single element.
lst = [1,4,5]arg_printer(lst)
([1, 4, 5],)
If we put an asterisk before lst, the values in the list will be unpacked and stored in args tuple separately.
lst = [1,4,5]arg_printer(*lst)
(1, 4, 5)
Example 8
We can pass multiple iterables to be unpacked together with single elements. All values will be stored in the args tuple.
lst = [1,4,5]
tpl = ('a','b',4)arg_printer(*lst, *tpl, 5, 6)
(1, 4, 5, 'a', 'b', 4, 5, 6)
Example 9
We can do the packing and unpacking with keyword arguments as well.
def arg_printer(**kwargs):
print(kwargs)
But the iterable that is passed as keyword arguments must be a mapping such as a dictionary.
dct = {'param1':5, 'param2':8}arg_printer(**dct)
{'param1': 5, 'param2': 8}
Example 10
If we also pass additional keyword arguments together with a dictionary, they will combined and stored in the kwargs dictionary.
dct = {'param1':5, 'param2':8}
arg_printer(param3=9, **dct){'param3': 9, 'param1': 5, 'param2': 8}
Conclusion
To summarize what we have covered:
- There are two types of arguments in a function which are positional arguments (declared by a name only) and keyword arguments (declared by a name and a default value).
- When a function is called, values for positional arguments must be given. Keywords arguments are optional (they take the default value if not specified).
- **args collects the positional arguments that are not explicitly defined and store them in a tuple
- **kwargs does the same as **args but for keyword arguments. They are stored in a dictionary because keyword arguments are stored as name-value pairs.
- Python does not allow positional arguments to follow keyword arguments. Thus, we first declare positional arguments and then keyword arguments.
Thank you for reading. Please let me know if you have any feedback.
References
https://calmcode.io/args-kwargs/introduction.html