LoginSignup
23
16

More than 5 years have passed since last update.

mapで利用する関数の引数を固定する

Last updated at Posted at 2014-11-29

関数の引数を固定する方法

mapを使うときに、一部の引数だけを固定したい場合がよくあるので、メモしておく。

functools.partialを使う。
このオブジェクトは引数付きで呼び出された関数のように振る舞う。

2つの引数を足し合わせて返す関数につくり、その後で片方の引数のみを固定する場合を考える。

In [1]: import functools

In [2]: def f(a, b):
   ...:         return a + b
   ...:

In [3]: map(functools.partial(f, b=1), [1, 2, 3])
Out[3]: [2, 3, 4]

参考

functools.partial

23
16
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
23
16