Skip to main content

Operator overloading for fast anonymous functions.

Project description

image image image image image image image image image

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, and resembles Scala's placeholders.

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. Kotlin uses it, but in Python it is a common short name for an iterator.

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

Performance

Every effort is made to optimize the placeholder instance. It's 20-40x faster than similar libraries on PyPI.

Placeholders are also iterable, allowing direct access to the underlying functions.

(func,) = _.age  # operator.attrgetter('age')

Performance should generally be comparable to inlined expressions, and faster than lambda. Below are some example benchmarks.

min(data, key=operator.itemgetter(-1))    # 1x
min(data, key=_[-1])                      # 1.3x
min(data, key=lambda x: x[-1])            # 1.6x

Installation

% pip install placeholder

Tests

100% branch coverage.

% pytest [--cov]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

placeholder-1.5.tar.gz (6.2 kB view details)

Uploaded Source

Built Distributions

placeholder-1.5-cp38-abi3-win_amd64.whl (11.0 kB view details)

Uploaded CPython 3.8+Windows x86-64

placeholder-1.5-cp38-abi3-win32.whl (10.6 kB view details)

Uploaded CPython 3.8+Windows x86

placeholder-1.5-cp38-abi3-musllinux_1_1_x86_64.whl (14.6 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.1+ x86-64

placeholder-1.5-cp38-abi3-musllinux_1_1_i686.whl (14.2 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.1+ i686

placeholder-1.5-cp38-abi3-musllinux_1_1_aarch64.whl (14.5 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.1+ ARM64

placeholder-1.5-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (12.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

placeholder-1.5-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.9 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64manylinux: glibc 2.5+ x86-64

placeholder-1.5-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (11.6 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ i686manylinux: glibc 2.5+ i686

placeholder-1.5-cp38-abi3-macosx_11_0_arm64.whl (8.1 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

placeholder-1.5-cp38-abi3-macosx_10_9_x86_64.whl (7.8 kB view details)

Uploaded CPython 3.8+macOS 10.9+ x86-64

File details

Details for the file placeholder-1.5.tar.gz.

File metadata

  • Download URL: placeholder-1.5.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for placeholder-1.5.tar.gz
Algorithm Hash digest
SHA256 d09583deca8beaa6c23cabe2cd8feba8faed356273c7dac613f92d1a8b16245b
MD5 1686a52a32a76da7815c4a1808dd5fa7
BLAKE2b-256 374d2bf88d8245a4328cd0f6adb24716d0c0b2736743a713daa7ef4028962c1a

See more details on using hashes here.

File details

Details for the file placeholder-1.5-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: placeholder-1.5-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for placeholder-1.5-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 8e2d00afd020d97833dfe01a6d0cbc1d9114da4d794b58f5e89bfccf792c75b0
MD5 dea61fc31f86033733222ce8896d7bd2
BLAKE2b-256 d867f93730336b223691712b25175b908c69ef918a83f0aa6aece54770c1045f

See more details on using hashes here.

File details

Details for the file placeholder-1.5-cp38-abi3-win32.whl.

File metadata

  • Download URL: placeholder-1.5-cp38-abi3-win32.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: CPython 3.8+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for placeholder-1.5-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 a59b9c323ae40f08f64d8de5e7e39132297309ae67f50873c18a72e36d1d0350
MD5 359cfb278e3f4358cd4190313c0a482a
BLAKE2b-256 f8d061c3240eb2d7a00cd3df355f4323e3b1f0dba3a04c9843b8b3fa8d5d5441

See more details on using hashes here.

File details

Details for the file placeholder-1.5-cp38-abi3-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for placeholder-1.5-cp38-abi3-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 d7f1aac0735b860e6871cb22cd6c38070aa0a0b923ae9a65c6c040576c91edcb
MD5 2e3db010e664dce235dd787a0d59ca9e
BLAKE2b-256 310defcca660df27c43e29693b308459581f73411ce5ed57fce6ec26f3754258

See more details on using hashes here.

File details

Details for the file placeholder-1.5-cp38-abi3-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for placeholder-1.5-cp38-abi3-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 495e4fb47d419a87268f1cae413f3dd040c6158f467f69545e3c45e8e1d2dc8a
MD5 17c7414cb8e17fcf3d57911684eca93d
BLAKE2b-256 7065829180da3ef1b0fee491172a7e2a331efef95d0236cf95c3fc7898b0b030

See more details on using hashes here.

File details

Details for the file placeholder-1.5-cp38-abi3-musllinux_1_1_aarch64.whl.

File metadata

File hashes

Hashes for placeholder-1.5-cp38-abi3-musllinux_1_1_aarch64.whl
Algorithm Hash digest
SHA256 a4a5cf9e3dcb5b91513e7461b5852166cec3b1716e0c138a65e79c59cc3c3fdd
MD5 8da8951671588a2cb3c2d335cf4879bb
BLAKE2b-256 2f8055178e72a2e7530d3f6f96549f7b6994e851b000488876b888cda0932fb0

See more details on using hashes here.

File details

Details for the file placeholder-1.5-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for placeholder-1.5-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d0cbb4dce6dff86907ff04b461d8eab987e24be53f4ee2d8352586decad89a71
MD5 cf61d6d6363b02d6664fd84e0a670701
BLAKE2b-256 cd97b01aeb990d8a6f334bfb0e918cd4f5b496513bd5e61eb89f5d9e00a5012a

See more details on using hashes here.

File details

Details for the file placeholder-1.5-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for placeholder-1.5-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ab843ac7df251ab6473bec1f5ba9347627925dbf1eb3374a68611ac0e5bad70b
MD5 f624a1e9c9da3dcc83f52cb710b2c223
BLAKE2b-256 19979a5dbd4ff4e10df0355d9ba99e43ec994dd37e13008faf6c5c15466ae2c1

See more details on using hashes here.

File details

Details for the file placeholder-1.5-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for placeholder-1.5-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 b877583ba7ed5514a4473a78eea98e7d55edbd34dea5c9a98ce15339b6fe8104
MD5 83d775e568772da24cc1b1eb2a8a69b7
BLAKE2b-256 19350a424184a42601b762af37a8d0d2f0157fb5226607d18ce88ea456654ef7

See more details on using hashes here.

File details

Details for the file placeholder-1.5-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for placeholder-1.5-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1dbd75adddf00c65540839c05576412d72d21ddeddcfed0284389f384fed0415
MD5 3385ab32dc0aaa2839ee72c18f79b30d
BLAKE2b-256 30fff0540aaa49a1136cf971e5a9bff33f705325658d3f1659649678edf47d87

See more details on using hashes here.

File details

Details for the file placeholder-1.5-cp38-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for placeholder-1.5-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bcca22fc9d54d7972de186fe8aa5416ca39fd0ba9ecd1895370207b0f25da622
MD5 0197d36a1686b717978620fc56924961
BLAKE2b-256 dedbf2340a0d080f3a68b089ec492a7da5724ca52d6586cd16fa6a24f8f4e8ae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page