2
3

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.

(個人メモ)pythonの可変長引数(**kwargs)にデフォルト変数を設定する方法

Last updated at Posted at 2021-09-09

pythonの可変長引数(**kwargs)にデフォルト変数を設定するにはpopを使用する。

def func(**kwargs):    
    key1=kwargs.pop('key1', 'default_key1')
    key2=kwargs.pop('key2', 'default_key2')
    print(key1)
    print(key2)    
    
if __name__=='__main__':
    kwd={'key1':1,
         'key2':2}
    func(**kwd) 
    
    kwd={}
    func(**kwd)   

出力

1
2
default_key1
default_key2
2
3
3

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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?