I’d like to propose adding a way for functions that take positional only arguments to be used with
As an example, say you want to create a function that parses datetimes for a specific format.
Due to positional only arguments, the following will not work:
>>> functools.partial(datetime.datetime.strptime, format="%d %B, %Y") >>> p("3 June, 2021") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: strptime() takes no keyword arguments
I’d like to add a
PLACEHOLDER (similar to c++ bind) sentinel in partial that allows to use the
functools.partial for positional only argument functions. Example:
from functools import partial p = partial( datetime.datetime.strptime, partial.PLACEHOLDER, "%d %B, %Y" ) p("3 June, 2019") # works as expected
With the introduction of PEP570, there are now even more functions that cannot be used with partiality due to the enforcement of positional arguments, making this even more useful IMHO.
I have a PoC here if you want to play with it for the Python part. If this is of interest I’ll create a bpo, add this to the extension as well and send a PR.