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(interval)

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

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.17.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

positional_defaults-2023.4.17-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file positional_defaults-2023.4.17.tar.gz.

File metadata

File hashes

Hashes for positional_defaults-2023.4.17.tar.gz
Algorithm Hash digest
SHA256 8fe283c5a007ea869396b6885b6d1fa54997a7c52ba186eaf17c2bcd15f24713
MD5 84e04fd531f4a3bc06c07767be26cb17
BLAKE2b-256 8be0c9132b18f7cc511d0dc1c31eb7d817aceb12c6a8798ac30519bbbb08f1d5

See more details on using hashes here.

File details

Details for the file positional_defaults-2023.4.17-py3-none-any.whl.

File metadata

File hashes

Hashes for positional_defaults-2023.4.17-py3-none-any.whl
Algorithm Hash digest
SHA256 74a94f165bf9845e3e10fe05b77b5e06fc18b3746cfcfc155366b0ee667b470c
MD5 ccfe286cc04599734a960c275bfe2679
BLAKE2b-256 cd0b90e7847baf2fb0f1895f0e6fea592d01fad7b1b758f5223fb568aa300223

See more details on using hashes here.

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