Can one partially apply the second argument of a function that takes no keyword arguments?

前端 未结 10 1631
太阳男子
太阳男子 2020-11-28 06:45

Take for example the python built in pow() function.

xs = [1,2,3,4,5,6,7,8]

from functools import partial

list(map(partial(pow,2),xs))

>&g         


        
10条回答
  •  一整个雨季
    2020-11-28 07:35

    The very versatile funcy includes an rpartial function that exactly addresses this problem.

    xs = [1,2,3,4,5,6,7,8]
    from funcy import rpartial
    list(map(rpartial(pow, 2), xs))
    # [1, 4, 9, 16, 25, 36, 49, 64]
    

    It's just a lambda under the hood:

    def rpartial(func, *args):
        """Partially applies last arguments."""
        return lambda *a: func(*(a + args))
    

提交回复
热议问题