Basically, what I’d like to do is:
>>> from functools import partial
>>> partial(str.startswith, prefix='a')('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: startswith() takes no keyword arguments
But more generally the question is, how to fill specific positional arguments with partial.
P.S. I do realize that I can use a lambda instead.
It cannot be done. You have to make a wrapper function.
Ostensibly, you would use keyword arguments, as you tried to do – that’s what they’re for, right? Unfortunately, as you’ve discovered, python’s standard library functions do not take named parameters. Thus, it is not possible given the current implementation of
partialwithout making another function to run interference.According to the acceptance of PEP 309, what was accepted for inclusion was "a partial() type constructor binding leftmost positional arguments and any keywords." Furthermore, it states:
Because additional positional arguments are appended, there would be no way to supply some preceding positional argument (by this implementation).
However, it goes on:
So, it apparently could be on the table, but as it stands, it is not implemented that way.
For the sake of disclosure, emphasis in quotes above was my own.