[Python] * args ** What is kwrgs?

01. What to write in this article

--What are the meanings of * and ** defined in function parameters and how to use them?

I would like to organize it here so that I can remember it quickly even if time passes.

02. * and ** are attached to parameters that receive variadic arguments.

I saw the code that says * args and ** kwargs in the function parameters. This is described in a parameter that receives variadic arguments, and the number of arguments passed to the parameter when calling a function can be any number instead of fixed.

Variadic arguments are those that allow you to specify any number of arguments where you can usually specify only one argument.


def kansu(*args):

kansu('Apple', 'Banana', 100) 
# ('Apple', 'Banana', 100)Received!

As for the parameter, 3 arguments are passed for only one ʻargs`, but it works without error.

In a familiar example, the print () function and the .format () method also use variadic arguments.

You can pass as many arguments as you like

print('a','b','c') # a b c

Also, the names ʻargs and kwargs` are customarily used, so you can use any name you like.

def x(*z):
    y = []
    for i in z:
# [10, 20, 30, 40, 50, 60, 70, 80]

03. Difference between when * is one and when it is two

There are two types of variable-length arguments, each of which has a different way of passing and receiving arguments.

    1. Parameters that accept variable-length positional arguments <-* args Receive any number of arguments as a tuple (if you don't pass any arguments, it will be an empty tuple) Pass as a positional argument
  1. Parameters that accept variable-length keyword arguments <-* kwargs Receive any number of arguments of the form keyword = value in dictionary type ( key value) (If you don't pass any arguments, it will be an empty dictionary) Pass as keyword argument

04. How to use parameters that receive variable length position arguments

By adding * to the argument, the parameter will receive a variable length position argument. That is, it receives any number of arguments. The usage of the function depends on where the variable-length positional argument is defined in the parameter of the function.

1.The last parameter is variable length

def myfunction(x, y, *z):
    print(f'{x} - {y} - {z}')

myfunction(2, 3, 5, 6, 7)  #  2 - 3 - (5, 6, 7)

The first and second arguments correspond to x and y, respectively. Subsequent arguments correspond to z and are stored as tuples.

2.Variable length parameters on the way

def myfunction(x, *y, z):
    print(f'{x} - {y} - {z}')

myfunction(2, 3, 5, 6, 7) # TypeError: myfunction() missing 1 required keyword-only argument: 'z'

It throws an error because no arguments have been passed to the parameter z. In this case, z is a keyword-only argument, so you must set a default value or use it as a keyword argument when calling.

2-1.Set default value

def myfunction(x, *y, z=0):
    print(f'{x} - {y} - {z}')

myfunction(2, 3, 5, 6, 7) # 2 - (3, 5, 6, 7) - 0

2-2.Use as a keyword argument

def myfunction(x, *y, z):
    print(f'{x} - {y} - {z}')

myfunction(2, 3, 5, 6, 7, z=0) # 2 - (3, 5, 6, 7) - 0

05. How to use variable length keyword arguments

By adding ** to the argument, the parameter receives a variable length keyword argument. That is, it receives an arbitrary number of keywords = values, and the keywords and arguments correspond to the dictionary-type key and value, respectively. How to use the function depends on where you define ** in the parameters of the function.

1.Make the last parameter a variable length keyword argument

def myfunction(param1, param2, **param):
    print(f'{param1} | {param2} | {param}')

myfunction(2, 3, param3=4, param4=5, param5=6)  # 2 | 3 | {'param3': 4, 'param4': 5, 'param5': 6}

The value obtained by the variable length keyword argument becomes a dictionary type.

2.Make the parameter in the middle a variable-length keyword argument

def myfunction(param1, **param, param2):
    print(f'{param1} | {param2} | {param}')

The result is a syntax error It is not allowed to define variable length keyword arguments in the middle.

06. When using two variable length arguments together

Normal parameters after parameters that receive variable-length positional arguments are keyword-only arguments.

def Test(a, *b, c): #c is for keywords only

There can be no regular parameters after the parameters that receive variable-length keyword arguments.


def myfunction(param1, **param, param2):
    print(f'{param1} | {param2} | {param}')

Therefore, when used together, the order must be as follows.

1.A parameter that receives both positional and keyword arguments
2.Parameters that receive variable-length positional arguments
3.Parameters that receive keyword-only arguments
4.Parameters that receive variable-length keyword arguments

Recommended Posts

[Python] * args ** What is kwrgs?
What is python
What is Python
[Python] What is Pipeline ...
[Python] What is virtualenv
[Python] Python and security-① What is Python?
What is a python map?
Python Basic Course (1 What is Python)
What is Python? What is it used for?
[Python] What is a zip function?
[Python] What is a with statement?
[Python] What is @? (About the decorator)
[python] What is the sorted key?
Python for statement ~ What is iterable ~
What is the python underscore (_) for?
Python> What is an extended slice?
What is namespace
What is copy.copy ()
[Python] What is pandas Series and DataFrame?
[Python] What is inherited by multiple inheritance?
What is NaN? NaN Zoya (Python) (394 days late)
Python is easy
What is Django? .. ..
What is dotenv?
What is POSIX?
What kind of programming language is Python?
What is klass?
What is SALOME?
What is "mahjong" in the Python library? ??
What is a dog? Python installation volume
What is Linux?
What is hyperopt?
Python is instance
What is Linux
What is pyvenv
What is __call__
What is Linux
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
What is "functional programming" and "object-oriented" in Python?
What is wheezy in the Docker Python image?
I tried Python! ] I graduated today from "What is Python! Python!"!
What are you comparing with Python is and ==?
[Introduction to Udemy Python 3 + Application] 54. What is Docstrings?
What are python tuples and * args after all?
Tell me what a conformal map is, Python!
What is a distribution?
What is Piotroski's F-Score?
What is Raspberry Pi?
What is Calmar Ratio?
What is a terminal?
[PyTorch Tutorial ①] What is PyTorch?
What is hyperparameter tuning?
What is a hacker?
What is JSON? .. [Note]
What is Linux for?
What is a pointer?
What is ensemble learning?
What is TCP / IP?
What is Python's __init__.py?
What is an iterator?
What is UNIT-V Linux?