Skip to content

coady/placeholder

Repository files navigation

image image image image build image CodeQL CodSpeed Badge image ty

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.

Usage

from placeholder import _     # single underscore

_.age < 18     # lambda obj: obj.age < 18
_[key] ** 2    # lambda obj: obj[key] ** 2

Note _ 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.

Performance

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')

Installation

pip install placeholder

Tests

100% branch coverage.

pytest [--cov]

About

Operator overloading for fast anonymous functions.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •