[Python] Function arguments * (star) and ** (double star)

In Python, when creating a function that accepts arbitrary arguments, prefix the arguments with \ * or \ * \ *.

From the function side, if you add \ *, it becomes Tuple, and if you add \ * \ *, it will be received in Dictionary. From the caller's point of view, when passing by Tuple, there is no Keyword, and when passing by Dictionary, there is Keyword.

def func1(*param1):
    print(param1)
def func2(**param1):
    print(param1)
In [3]: func1(1,2,3,4,5)
(1, 2, 3, 4, 5)

In [4]: func2(a=1,b=2,c=3,d=4,e=5)
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}

Unpacking Argument When you want to hand over the contents of tuple separately The results of func1 (* t1) and func1 (0,1,2) are the same. For Dictionary, use **.

In [13]: t1 =  (0,1,2)    

In [14]: func1(*t1)
(0, 1, 2)

In [15]: func1(0,1,2)
(0, 1, 2)

In [16]: func1(t1)
((0, 1, 2),)

Details can be found here [https://docs.python.org/dev/tutorial/controlflow.html#keyword-arguments).

Recommended Posts

[Python] Function arguments * (star) and ** (double star)
About function arguments (python)
Python: About function arguments
Included notation in Python function arguments
[Python] function
Function synthesis and application in Python
[Python] Difference between function and method
python function ②
Combine python function arguments into one variable
python enumerate function
Python> function> Closure
[Python] Generator function
Python> function> Inner function
Python function decorator
Example of taking Python> function> * args as arguments
I compared "python dictionary type" and "excel function"
I tried function synthesis and curry with python
[Python] How to use hash function and tuple.
[python] Compress and decompress
[Python for Hikari-] Chapter 06-04 Functions (arguments and return value 3)
Python and numpy tips
[Python] pip and wheel
Function execution time (Python)
Batch design and python
Python iterators and generators
Introduce pipe operators and function synthesis to Python (provisional)
Ruby, Python and map
Python function argument summary
python input and output
Python and Ruby split
Crawling with Python and Twitter API 1-Simple search function
Python print function (sequel)
Time floor function (Python)
[Python for Hikari-] Chapter 06-01 Functions (Intrinsic Functions and Function Definitions)
Execute Python function from Powershell (how to pass arguments)
Summary of Python arguments
[Python for Hikari-] Chapter 06-03 Functions (arguments and return value 2)
Python3, venv and Ansible
Python asyncio and ContextVar
Associate Python Enum with a function and make it Callable
[Introduction to Udemy Python3 + Application] 50. Positional arguments, keyword arguments, and default arguments
[Python for Hikari-] <Supplement> Chapter 06-05 Functions (arguments and return values 4)
Programming with Python and Tkinter
Encryption and decryption with Python
Python: Class and instance variables
3-3, Python strings and character codes
Python 2 series and 3 series (Anaconda edition)
Create a function in Python
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Python indentation and string format
Python real division (/) and integer division (//)
Install Python and Flask (Windows 10)
Use callback function in Python
About python objects and classes
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
ntile (decile) function in python
[Python] Etymology of python function names
About python beginner's memorandum function