pythonメモ -- 関数編
関数に渡す値を辞書型にして渡す方法
結果的には test(**dic) のように渡せば良い
以下実験
>>> h = dict(req="",arg='aaa') >>> test(h) >>> test1(h) >>> test1(*h) arg >>> test1(*h) arg >>> test(*h) arg >>> h {'req': '', 'arg': 'aaa'} >>> h = dict(req="",arg='aaa', arg2 = 'bbb') >>> test1(*h) req arg >>> test1(**h) aaa bbb >>> test(**h) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: test() got an unexpected keyword argument 'arg2'
引数が(*h)の時は、引数の順番通りにdictが配列に展開されて渡されているけど、(**h)の時は関数の引数名がkey:valueで渡される。