functools.Placeholder was added in Python 3.14 to work with functools.partial. functools.partial performs partial function application by freezing positional or keyword arguments. Prior to Python 3.14 and up through 3.13, partial could freeze arguments by passing positional arguments or keyword arguments. Passing keyword arguments allows freezing parameters that are not first in a function signature, for example int(x, base=10) can be partially applied with base=2 to create a binary converter. partial(int, 2) fails because the positional 2 binds to x; using partial(int, base=2) succeeds. Keyword-based freezing does not always work in all call signatures.
The new Placeholder, added in Python 3.14, only makes sense in the context of functools.partial, so in order to understand Placeholder you will need to understand how functools.partial works and how to use it. In a nutshell, partial allows you to perform partial function application, by "freezing" arguments to functions. How to pass arguments to functools.partial Up until Python 3.13, you could use partial to freeze arguments in two types of ways: you could pass positional arguments to partial, which would be passed in the same order to the function being used with partial; or you could pass keyword arguments to partial, which would be passed with the same name to the function being used with partial.
Using keyword arguments to skip the first argument The method 2. is especially useful if you're trying to freeze an argument that is not the first one. For example, if you use the built-in help on the built-in int, you can see this signature: int(x, base=10) -> integer If you want to convert a binary string to an integer, you can set base=2: print(int("101", 2)) # 5 Now, suppose you want to create a function from_binary by "freezing" the argument 2 in the built-in int. Writing from_binary = partial(int, 2) won't work, since in partial(int, 2), the value 2 is seen as the argument x from the signature above. However, you can pass the base as a keyword argument, skipping the first argument x from the signature of the built-in int: from functools import partial from_binary = partial(int, base=2) print(from_binary("101")) # 5
Collection
[
|
...
]