Help us understand the problem. What is going on with this article?

キーワード専用引数

More than 1 year has passed since last update.

環境

python3

キーワード引数

def func1(value1, value2):
  print(f'{value1}, {value2}')

渡された引数を表示するだけの簡単な関数func1は以下の呼び出し方のいずれでも呼び出せる。

func1(1, 'a')
func1(value1=1, value2='a')
func1(1, value2='a')

キーワード専用引数

def func2(value1, *, value2):
  print(f'{value1}, {value2}')

この関数の定義のように引数の宣言に*をつけた場合は、*以降の引数はキーワード引数にしないと呼び出せなくなる。

func2(1, value2='a')        # 呼び出しOK
func2(1, 'a')               # エラー

引数が多い場合とか、同じような値を複数渡す場合には、キーワード専用引数にしたほうが意図がわかりやすくなる。

nittyan
Pythonが好きです。Pythonで仕事してます。 仕事で使えるであろうレベルの言語は、Java、Python。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした