A placeholder uses operator overloading to create partially bound functions on-the-fly. When used in a binary expression, it will return a callable object with the other argument bound. It's useful for replacing lambda in functional programming.
from placeholder import _ # single underscore
_.age < 18 # lambda obj: obj.age < 18
_[key] ** 2 # lambda obj: obj[key] ** 2Note _ has special meaning in other contexts, such as the previous output in interactive shells. Assign to a different name as needed.
_ is a singleton of an F class, and F expressions can also be used with functions.
from placeholder import F
-F(len) # lambda obj: -len(obj)All applicable double underscore methods are supported. Some methods coerce types: len, bool, in.
The placeholder instance leverages functools.partial and Placeholder for optimization. It is significantly faster than similar libraries on PyPI.
Performance should generally be comparable to inlined expressions, and faster than lambda. There is an optional legacy C extension which provides a small speedup for right-bound functions in Python <3.14 only.
Placeholders are provisionally iterable, allowing access to the underlying function and bound args.
(func,) = _.age # operator.attrgetter('age')pip install placeholder100% branch coverage.
pytest [--cov]