[PYTHON] Notes on * args and ** kargs

I often forget it, so make a note.

Variadic argument

If * args is specified as an argument, it will receive an arbitrary number of arguments and become a tuple.

>>> def func(a, b, *args):
...     print('a: {}'.format(a))
...     print('b: {}'.format(b))
...     print('*args: {}'.format(args))
...
>>> func(1, 2, 3, 4, 5, 6)
a: 1
b: 2
*args: (3, 4, 5, 6)

If ** kargs is specified as an argument, it will receive an arbitrary number of keyword arguments and become a dictionary.

>>> def func(a, b, **kargs):
...     print('a: {}'.format(a))
...     print('b: {}'.format(b))
...     print('**kargs: {}'.format(kargs))
...
>>> func(1, 2, x=3, y=4, z=5)
a: 1
b: 2
**kargs: {'z': 5, 'x': 3, 'y': 4}

Unpack assignment

If you add * to the list and pass it as an argument, it will be unpacked and assigned to the argument.

>>> args = [1, 2, 3]
>>> def func(a, b, c):
...     return a + b + c
...
>>> func(*args)
6

If you add ** to the dictionary and pass it to the argument, it will be unpacked and assigned to the argument.

>>> kargs = {'a': 3, 'b': 4, 'c': 5}
>>> def func(a, b, c):
...     return a + b + c
...
>>> func(**kargs)
12

Such a thing

>>> args = [3, 4, 5]
>>> def func(a, b, *args):
...     print('a: {}'.format(a))
...     print('b: {}'.format(b))
...     print('*args: {}'.format(args))
...
>>> func(1, 2, *args)
a: 1
b: 2
*args: (3, 4, 5)

It is unpacked by passing it as an argument with * args, and the function receives it as a variadic argument, so it becomes a list. It's a story that you can give it as a list from the beginning, but you can also do this.

Reference site

* args ** Meaning of kwargs

Recommended Posts

Notes on * args and ** kargs
Notes on pyenv and Atom
Notes on Python and dictionary types
Notes on using post-receive and post-merge
Notes on Flask
Notes on building Python and pyenv on Mac
Notes on installing Python3 and using pip on Windows7
Notes on neural networks
Celery notes on Django
Notes on installing PycURL
Notes on using Alembic
Notes on SciPy.linalg functions
Notes and Tips on Vertical Joining of PySpark DataFrame
Notes on HDR and RAW image processing with Python
Notes on building TinyEMU and booting the Linux kernel on Emscripten
Python on Ruby and angry Ruby on Python
Notes on installing dlib on mac
Notes on python's sqlite3 module
python * args, ** kwargs Usage notes
Notes on defining PySide slots (2)
[Django] Notes on using django-debug-toolbar
Recording and playback on Linux
Notes on defining PySide slots
[Python] Notes on data analysis
Notes on optimization using Pytorch
Notes on installing Python on Mac
Notes on studying multidimensional scaling
Optional arguments and * args, ** kwargs
Notes on installing pipenv on Mac
Notes on deploying pyenv with Homebrew and managing Python versions
Catalina on Mac and pyenv
Notes on installing Anaconda 3 on Windows
Notes on imshow () in OpenCV
[Python] Notes on while statements (writing style and infinite loop)
Notes on installing Python on CentOS
Notes on reading and writing float32 TIFF images in python
Python 3.6 on Windows ... and to Xamarin.
MQTT on Raspberry Pi and Mac
Install Mecab and mecab-python3 on Ubuntu 14.04
Notes on using MeCab from Python
[Golang] Notes on frequently used functions
Notes on how to use pywinauto
Build and install OpenCV on Windows
Survey on building and running kivi
Integrate Modelica and Python on Windows
Notes on how to use featuretools
For me: Infrastructure and network notes
Notes on installing Python using PyEnv
Install fabric on Ubuntu and try
Mastering pip and wheel on windows
Error classification (python3.x) and Debugging notes
Notes on using rstrip with python.
Notes on accessing dashDB from python
(Personal notes) Python metaclasses and metaprogramming
Homebrew and Pycharm installation instructions notes
Notes on how to use doctest
Notes on using matplotlib on the server
Notes on how to write requirements.txt
Notes on installing Ubuntu 18.04 on the XPS 15 7590
(Beginner) Notes on using pyenv on Mac
Install easy_install and pip on windows