キーワード引数リストのアンパック

投稿者: | 9月 3, 2022
In [1]: dct = {'comment1' : 'WILL GET', 'comment2' : 'A CERTIFICATION' }

In [2]: def declaration(who, comment1 = 'will get', comment2 = 'a certification.'):
    ...:     print(who, comment1, comment2)
    ...: 

In [3]: declaration('MASH', **dct)
MASH WILL GET A CERTIFICATION

In [4]: declaration('MASH', dct)    #キーワード引数がアンパックされず、第2引数に変数dctがそのまま展開されている。
MASH {'comment1': 'WILL GET', 'comment2': 'A CERTIFICATION'} a certification.

In [5]: declaration('MASH')         #位置引数のwhoのみ指定され、第2、第3引数は、デフォルトが適用されている。
MASH will get a certification.