5
12

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 5 years have passed since last update.

pythonの関数の引数を1つの変数にまとめる

Posted at

こんにちはsekitakaです。
pythonで引数の数が多い関数で引数を1つのdictにまとめて使用する方法です。
**演算子を使用することでdictが展開できるみたいなので、それを利用して以下のようにかけます。

def my_function(foo, bar):
    print "foo is {0}, bar is {1}".format(foo, bar)


params = {
    'foo': 'FOO',
    'bar': 'BAR',
}

# paramsのfooを引数のfooとして、barも同様に渡す
my_function(**params)

ifで一部の引数の値だけ変更とかしやすくていいですね。

5
12
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
5
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?