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

前端 未结 10 1677
太阳男子
太阳男子 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:33

    I think I'd just use this simple one-liner:

    import itertools
    print list(itertools.imap(pow, [1, 2, 3], itertools.repeat(2)))
    

    Update:

    I also came up with a funnier than useful solution. It's a beautiful syntactic sugar, profiting from the fact that the ... literal means Ellipsis in Python3. It's a modified version of partial, allowing to omit some positional arguments between the leftmost and rightmost ones. The only drawback is that you can't pass anymore Ellipsis as argument.

    import itertools
    def partial(func, *args, **keywords):
        def newfunc(*fargs, **fkeywords):
            newkeywords = keywords.copy()
            newkeywords.update(fkeywords)
            return func(*(newfunc.leftmost_args + fargs + newfunc.rightmost_args), **newkeywords)
        newfunc.func = func
        args = iter(args)
        newfunc.leftmost_args = tuple(itertools.takewhile(lambda v: v != Ellipsis, args))
        newfunc.rightmost_args = tuple(args)
        newfunc.keywords = keywords
        return newfunc
    
    >>> print partial(pow, ..., 2, 3)(5) # (5^2)%3
    1
    >>> print partial(pow, 2, ..., 3)(5) # (2^5)%3
    2
    >>> print partial(pow, 2, 3, ...)(5) # (2^3)%5
    3
    >>> print partial(pow, 2, 3)(5) # (2^3)%5
    3
    

    So the the solution for the original question would be with this version of partial list(map(partial(pow, ..., 2),xs))

提交回复
热议问题