0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

引数がいくつ入るか分からない場合2 **kwargs 可変長引数

Last updated at Posted at 2020-01-09

引数がいくつ入るか分からない場合 *args 可変長引数は、
タプルでしたが、
キーワード引数を辞書化して同じ様な処理をする。

辞書
def players(**kwargs):
    for k, v in kwargs.items():
        print(k + '' + v + 'です。')

d = {
    '太郎':'勇者',
    '次郎':'戦士',
    '三郎':'魔法使い',
    '四郎':'僧侶'
}

players(**d)

先程と同様、辞書dを
**dで展開し、
players関数に渡す。
**kwargsで辞書化。
そして、
その辞書kwargsを
for k, v in kwargs.items():
print(k + 'は' + v + 'です。')で
利用。

辞書の実行結果
太郎は勇者です。
次郎は戦士です。
三郎は魔法使いです。
四郎は僧侶です。
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?