Summary of Python arguments

Introduction

Although it is a python argument, it is a positional argument, a keyword argument, and a keyword-only argument. I've been confused about getting multiple arguments using \ *, **, so I've organized them.

Positional argument

def f(a, b):
  return a, b
f(1, 2)
=======================
(1, 2)

Assign by specifying an argument (keyword argument)

Arguments passed like a = 1 are called keyword arguments, and the order in which they are passed is irrelevant.

def f(a, b):
  return a, b
f(b=2, a=1)
=======================
(1, 2)

You cannot pass a positional argument after a keyword argument.

def f(a, b):
  return a, b
f(b=2, 1)
=======================
SyntaxError: positional argument follows keyword argument

Receive variadic arguments as tuples and dicts using \ *, **

You can use * to pass variadic arguments as tuples.

def f(a, b, *c):
  return a, b, c
f(1, 2, 3, 4)
=======================
(1, 2, (3, 4))

You can use ** to pass variable length keyword arguments as a dict.

def f(a, b, **c):
  return a, b, c
f(1, 2, x=3, y=4)
=======================
(1, 2, {'x': 3, 'y': 4})

Positional arguments cannot be assigned after arguments using \ *.

def f(a, *b, c):
  return a, b, c
f(1, 2, 3, 4)
=======================
TypeError: f() missing 1 required keyword-only argument: 'c'

It seems that neither positional arguments nor keyword arguments can be assigned after **.

def f(a, **b, c):
  return a, b, c
f(1, x=2, y=3, c=4)
=======================
SyntaxError: invalid syntax

Keyword-only arguments

You can now specify keyword-only arguments from Python 3. Arguments that come after the argument with * at the beginning can only be assigned as keyword arguments. In the following, c should be assigned as a keyword argument instead of a positional argument.

def f(a, *b, c):
  return a, b, c
f(1, 2, 3, c=4)
=======================
(1, (2, 3), 4)

If you do not use variable-length positional arguments, specify * alone, and the subsequent arguments become keyword-only arguments.

def f(a, *, b):
  return a, b
f(1, 2)
=======================
TypeError: f() takes 1 positional argument but 2 were given
def f(a, *, b):
  return a, b
f(1, b=2)
=======================
(1, 2)

Position-only arguments

As I told you in the comments, it seems that there are also position-only arguments. https://docs.python.org/ja/3/tutorial/controlflow.html#positional-only-parameters

def f(x, y):
  print(x, y)
f(y=456, x=123)
============================
123 456

def f(x, y, /):
  print(x, y)
f(y=456, x=123)
============================
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() got some positional-only arguments passed as keyword arguments: 'x, y'

f(x=123, y=456)
============================
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() got some positional-only arguments passed as keyword arguments: 'x, y'

f(123, 456)
============================
123 456

When I was pointed out by other people, I thought that it might be a good study because it was embarrassing.

Recommended Posts

Summary of Python arguments
Summary of python file operations
Summary of Python3 list operations
Python summary
A brief summary of Python collections
Summary of Python indexes and slices
[OpenCV; Python] Summary of findcontours function
Introduction of Python
Python tutorial summary
Basics of Python ①
Basics of python ①
Copy of python
python related summary
Python basics summary
Introduction of Python
[Python] Summary of how to use pandas
Summary of various for statements in Python
[Python] Summary of array generation (initialization) time! !! !!
[Python2.7] Summary of how to use unittest
Summary of built-in methods in Python list
Summary of useful techniques for Python Scrapy
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
Axis option specification summary of Python "numpy.sum (...)"
Numerical summary of data
[Python] Operation of enumerate
Summary about Python scraping
About function arguments (python)
Python Django tutorial summary
Correspondence summary of array operation of ruby and python
Summary of Tensorflow / Keras
Unification of Python environment
Summary of the differences between PHP and Python
Copy of python preferences
Summary of how to import files in Python 3
Basics of Python scraping basics
Summary of pyenv usage
[python] behavior of argmax
Summary about Python3 + OpenCV3
Usage of Python locals ()
the zen of Python
Summary of how to use MNIST in Python
Python function argument summary
Installation of Python3 and Flask [Environment construction summary]
Installation of Python 3.3 rc1
Python directory operation summary
Python AI framework summary
Python iteration related summary
Summary of string operations
[Python] Summary of S3 file operations with boto3
# 4 [python] Basics of functions
Summary of frequently used Python arrays (for myself)
Basic knowledge of Python
Sober trivia of python3
Summary of studying Python to use AWS Lambda
I / O related summary of python and fortran
Basics of python: Output
Summary of logrotate software logrotate
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Dictionary of keyword arguments