How to fill specific positional arguments with partial in python?

后端 未结 3 1703
悲&欢浪女
悲&欢浪女 2020-12-08 13:18

Basically, what I\'d like to do is:

>>> from functools import partial
>>> partial(str.startswith, prefix=\'a\')(\'a\')
Traceback (most rece         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 13:44

    If you really need this you can use rpartial from funcy 3rd-party library.

    Its code is here:

    def rpartial(func, *args):
        return lambda *a: func(*(a + args))
    

    So, your case can be handled as following:

    >>> startswith_a = rpartial(str.startswith, 'a')
    >>> startswith_a('abc')
    True
    >>> startswith_a('def')
    False
    

提交回复
热议问题