Last updated:
0 purchases
positionaldefaults 2023.4.19
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.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.