Skip to main content

Set defaults for any positional-only parameter

Project description

positional_defaults

Python package to set defaults for any positional-only parameter

This tiny Python package contains a decorator @defaults which lets you specify default arguments for any positional-only parameter, no matter where it appears in the argument list.

Installation

pip install positional_defaults

Usage

Use the @defaults decorator to set default values for positional-only parameters (i.e. those before the positional-only indicator /):

from positional_defaults import defaults

@defaults(start=0)
def myrange(start, stop, /, step=1):
    ...

# now these are equivalent
myrange(4)
myrange(0, 4)
myrange(0, 4, 1)

This works on methods as well:

class A:
    @defaults(start=0)
    def myrange(self, start, stop, /, step=1):
        ...

Multiple defaults can be set, which are filled in the order in which they are specified:

@defaults(forename='Alice', greeting='Welcome', prefix='Mrs')
def greet(greeting, prefix, forename, surname, /, suffix='Esq'):
    ...

# these are now equivalent
greet('Smith')
greet('Alice', 'Smith')
greet('Welcome', 'Alice', 'Smith')
greet('Welcome', 'Mrs', 'Alice', 'Smith')
greet('Welcome', 'Mrs', 'Alice', 'Smith', 'Esq')

Signatures

Left-defaulted functions come with the correct signature:

>>> from inspect import signature
>>> signature(myrange)
<Signature (start=0, stop, /, step=1)>
>>> signature(greet)
<Signature (greeting='Welcome', prefix='Mrs', forename='Alice', surname, /, suffix='Esq')>

These show up correctly in the usual places such as help():

>>> help(myrange)

Help on function myrange:

myrange(start=0, stop, /, step=1)

>>> help(greet)

Help on function greet:

greet(greeting='Welcome', prefix='Mrs', forename='Alice', surname, /, suffix='Esq')

Performance

When the package is compiled as a native extension (which is what pip will deliver in the majority of cases), functions with positional defaults have comparable performance with respect to their undecorated functions.

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

positional_defaults-2023.4.19.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distributions

positional_defaults-2023.4.19-py3-none-any.whl (4.7 kB view hashes)

Uploaded Python 3

positional_defaults-2023.4.19-cp311-cp311-win_amd64.whl (10.2 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

positional_defaults-2023.4.19-cp311-cp311-win32.whl (9.6 kB view hashes)

Uploaded CPython 3.11 Windows x86

positional_defaults-2023.4.19-cp311-cp311-musllinux_1_1_x86_64.whl (23.1 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

positional_defaults-2023.4.19-cp311-cp311-musllinux_1_1_i686.whl (22.7 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

positional_defaults-2023.4.19-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.7 kB view hashes)

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

positional_defaults-2023.4.19-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (17.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

positional_defaults-2023.4.19-cp311-cp311-macosx_10_9_x86_64.whl (7.1 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

positional_defaults-2023.4.19-cp310-cp310-win_amd64.whl (10.2 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

positional_defaults-2023.4.19-cp310-cp310-win32.whl (9.6 kB view hashes)

Uploaded CPython 3.10 Windows x86

positional_defaults-2023.4.19-cp310-cp310-musllinux_1_1_x86_64.whl (21.5 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

positional_defaults-2023.4.19-cp310-cp310-musllinux_1_1_i686.whl (21.1 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

positional_defaults-2023.4.19-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.9 kB view hashes)

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

positional_defaults-2023.4.19-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (16.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

positional_defaults-2023.4.19-cp310-cp310-macosx_10_9_x86_64.whl (7.1 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

positional_defaults-2023.4.19-cp39-cp39-win_amd64.whl (10.2 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

positional_defaults-2023.4.19-cp39-cp39-win32.whl (9.6 kB view hashes)

Uploaded CPython 3.9 Windows x86

positional_defaults-2023.4.19-cp39-cp39-musllinux_1_1_x86_64.whl (21.3 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

positional_defaults-2023.4.19-cp39-cp39-musllinux_1_1_i686.whl (20.9 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

positional_defaults-2023.4.19-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.7 kB view hashes)

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

positional_defaults-2023.4.19-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (16.4 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

positional_defaults-2023.4.19-cp39-cp39-macosx_10_9_x86_64.whl (7.1 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

positional_defaults-2023.4.19-cp38-cp38-win_amd64.whl (10.2 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

positional_defaults-2023.4.19-cp38-cp38-win32.whl (9.6 kB view hashes)

Uploaded CPython 3.8 Windows x86

positional_defaults-2023.4.19-cp38-cp38-musllinux_1_1_x86_64.whl (21.6 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

positional_defaults-2023.4.19-cp38-cp38-musllinux_1_1_i686.whl (21.3 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

positional_defaults-2023.4.19-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.5 kB view hashes)

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

positional_defaults-2023.4.19-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (17.2 kB view hashes)

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

positional_defaults-2023.4.19-cp38-cp38-macosx_10_9_x86_64.whl (7.1 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

Supported by

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