python * args, ** kwargs Usage notes

What can i do

You can receive all the arguments that you do not know how many will come.

Positional argument

python


def test(*args, **kwargs):
    print(args)
    print(kwargs)
    
test(1, 2, 'hoge')

output


(1, 2, 'hoge')
{}

Keyword arguments

** Enter kwargs with dict ().

python


def test(*args, **kwargs):
    print(args)
    print(kwargs)
    
test(1, 2, 3, 4, 5, col=4, row=5)

output


(1, 2, 3, 4, 5)
{'col': 4, 'row': 5}

If there is no corresponding argument

It will be an empty tuple () and an empty dict ().

python


def test(*args, **kwargs):
    print(args)
    print(kwargs)
    
test()

output


()
{}

Convenient to use with wrapper function

You can pass the received argument as it is

python


def func1(a, b, c=2):
    d = a + b * c
    return d

def func2(*args, **kwargs):
    d = func1(*args, **kwargs)
    return d

print(func2(1, 2))
print(func2(1, 2, c=5))

output


5
11

Argument name can be changed

In fact, if the number of * is the same, the argument name can be changed freely. You can use * a, * hoge instead of * args, and ** b, ** fuga etc. instead of ** kwargs.

python


def test(*a, **b):
    print(a)
    print(b)
    
test(1, 2, 3, 4, 5, col=4, row=5)

output


(1, 2, 3, 4, 5)
{'col': 4, 'row': 5}

Let's try!

Recommended Posts

python * args, ** kwargs Usage notes
python decorator usage notes
[Python] pytest-mock Usage notes
Python standard unittest usage notes
Python scraping notes
Python study notes _000
Python learning notes
concurrent.futures Usage notes
Python beginner notes
Python study notes_006
python C ++ notes
Python study notes _005
Python grammar notes
Python Library notes
python personal notes
python pandas notes
Python study notes_001
python learning notes
Python3.4 installation notes
missingintegers python personal notes
Python package development notes
Usage of Python locals ()
Python ipaddress package notes
[Personal notes] Python, Django
Python Pickle format notes
First Python miscellaneous notes
Matlab => Python migration notes
Notes around Python3 assignments
Notes using Python subprocesses
Python try / except notes
Python framework bottle notes
Python notes using perl-ternary operator
[Python] Correct usage of map
Web scraping notes in python3
Python notes to forget soon
Notes on * args and ** kargs
Python notes using perl-special variables
Python 處 處 regular expression Notes
Python Tkinter notes (for myself)
Convenient diff command usage notes
[Python] Notes on data analysis
Python data analysis learning notes
Notes on installing Python on Mac
Optional arguments and * args, ** kwargs
[Python] * args ** What is kwrgs?
Sample usage of Python pickle
Basic usage of Python f-string
Get Evernote notes in Python
[Python] Correct usage of join
Notes on installing Python on CentOS
Notes on Python and dictionary types
Minimum grammar notes for writing Python
Notes on using MeCab from Python
Personal notes for python image processing
Python Pandas Data Preprocessing Personal Notes
Typing automation notes by Python beginners
Notes for me python csv graph
Notes on installing Python using PyEnv
Elementary ITK usage learned in Python
Notes for Python file input / output
Notes on using rstrip with python.