positionnal only arguments/parameters
Project description
positionnal only arguments/parameters
=====================================
Module to fake positional only arguments (pep 497, Draft).
Let's take the following function as example:
```python
def diff(left, right, context=3):
'''print the diff between `left` and `right` with `context`
'''
...
```
`right` and `left` are now part of the API, and users
may decide to use `diff(left=str1, right=str1)`, blocking
api changes, so you can't change the names of the parameters,
to for example `def diff(old, new, context=3)` without using
the C-api.
This module provide a decorator @positionalonly to make all the
default-less parameters of your function positional only, so you
can write:
@positionalonly
def diff(left, right, context=3):
...
Usingit like will raise a TypeError, and the function
signature will use the positional only argument syntax
with the `/` of pep 497.
>>> diff(right="bar", left="foo")
TypeError: The following parameters of `diff` are positional only.
They were used as keywords arguments:
- 'right' ('bar') should be in 2nd position
- 'left' ('foo') should be in 1st position
>>> help(diff)
Help on function diff in module __main__:
diff(left, right, /, context=3)
Advance usage
=============
By default `positionalonly` will make the limit between positional-only and the
rest of the arguments just before the first argument with default. You can
tweek this behavior by passing an integer as parameter indicating the number of
argument to make positional-only:
>>> @positionalonly(3)
... def diff(old, new, context=3)
... pass
>>> signature(diff)
diff(left, right, context=3, /)
>>> @positionalonly(1)
... def diff(old, new, context=3)
... pass
>>> signature(diff)
diff(left, /, right, context=3)
If you are too lazy to count, or are afraid of misscounting, `positionalonly`
will also decide to put the limit to the first argument which default value is
`positionalonly` itself.
>>> @positionalonly
... def diff(old, new, end=positionalonly, context=3)
... pass
>>> signature(diff)
diff(left, right, /, context=3)
>>> pos_or_kw
>>> @positionalonly
... def diff(old, start=pos_or_kw, context=3)
... pass
>>> signature(diff)
diff(left, /, right, context=3)
Reasons to use positional only
==============================
Naming of argument, and in particular the function
signature can be helfull for tab completion, and static analysis
to understand the code. While posistional only is usable using
`*args` it hides all this information both from the human and
the computer.
The default behavior of Python, is to make agument by dafult,
positional an keyword.
This fact often prevent the developer from changing the
name without breaking the API.
This also prevent arbitrary `**kwargs` keys as they are
be taken by "positionaly or keyword" arguments.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file positionalonly-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: positionalonly-0.0.1-py3-none-any.whl
- Upload date:
- Size: 74.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f999e0b2677af6cb1e02844b2c8759329d0c2d38d8487e14250945356b6e5d72 |
|
MD5 | 58f0de155a2eca5ae8804ad31f938eb1 |
|
BLAKE2b-256 | 195f1a0c721479bd45f85e69cf2aa44f6ea13cf75eb6d829105fddc8edaddf01 |