Indexing with keyword arguments
Project description
PEP 472 -- Support for indexing with keyword arguments
This package provides experimental support for indexing with keyword argments. This is proposed in PEP 637. See also PEP 637's predecessor, PEP 472.
We can write
fn(1, 2, a=3, b=4)
and some users wish to be able to write
d[1, 2, a=3, b=4]
particularly for use in data science and annotations.
Helper function o
The module provide a helper function
from kwkeys import o
such that
d[o(1, 2, a=3, b=4)]
works as well as can be reasonably expected, or so the author thinks.
In module kwkey/example_jfine.py there's an implementation of a class PointMap. It is a subclass of dict which supports namedtuple style access to keys.
My approach uses a helper class K for implementing these experiments. Some wish for the syntax of Python to be extended, to allow keyword arguments in the d[...] syntax.
If the syntax is extended, I believe a helper class such as K would be a good idea. Steven D'Aprano believe that K is irrelevant, except for experimentation.
This package, once refined and production ready, could be used to allow the the style of the new syntax to be used with current Python, via
d[o(1, 2, a=3, b=4)]
instead of
d[1, 2, a=3, b=4]
Items-key duality
This package, from version 0.0.2, also provides items-key
duality. Roughly speaking, duality is writing key[items]
instead of
items[key]
. Python already supports something like this:
>>> from operator import itemgetter
TypeError: 'operator.itemgetter' object is not subscriptable
>>> itemgetter(3)('abcde')
'd'
To provide duality we need a special class X
such that
X(1, 2, a=3, b=4)[items]
is equivalent (for the chosen future semantics of keyword arguments) to:
items[1, 2, a=3, b=4]
In current Python the following three statements are already valid syntax.
X(1, 2, a=3, b=4)[items] = val
val = X(1, 2, a=3, b=4)[items]
del X(1, 2, a=3, b=4)[items]
PEP 637 proposes that we allow keywords inside the square brackets
[...]
. Using items-key duality, we can move the keywords to outside
the square brackets, into a context where they are allowed.
Semantics of keyword arguments
For this to work, a suitable class X
must be provided, that
implements the desired semantics. This package provides three such
classes.
Class A
- the current semantics
Here's an example. For more, see file kwkey/test_duality.py
.
>>> A(1, 2)[log] = 'val'
log: setitem(*((1, 2), 'val'), **{})
This means that the following three statements are equivalent.
log[(1, 2)] = 'val'
A((1, 2))[log] = 'val'
log.__setitem__((1, 2), 'val')
Class B
- the semantics proposed by D'Aprano and von Rossum
>>> B(1, 2, a=3, b=4)[log]
log: getitem(*((1, 2),), **{'a': 3, 'b': 4})
The previous setitem call is equivalent to
setitem((1, 2), 'val', a=3, b=4)
When no keywords are present, this give the same semantics as class
`A`.
### Class `C` - the semantics propose by Fine
Here the semantics of keywords depend on a special attribute of
`items`, which we call `keyfn`. (Strictly speaking, it should be an
attribure of `type(items)`.
#### Default
The default is to give the same semantics as class `A`.
```python
>>> C((1, 2))[log] = 'val'
log: setitem(*((1, 2), 'val'), **{})
The previous setitem call is equivalent to
setitem((1, 2), 'val')
None
If the special attribute keyfn
is None
we get
>>> C(1, 2, a=1, b=2)[nokey] = 'val'
log: setitem(*('val', 1, 2), **{'a': 3, 'b': 4})
The previous setitem call is equivalent to
setitem('val', 1, 2, a=3, b=4)
User supplied keyfn
The class type(items)
can supply its own keyfn
, to provide custom
behaviour. Here's an example (see kwkey/test_duality.py
for
details).
>>> C(1, 2, a=1, b=2)[k_log] = 'val'
log: setitem(*(K(1, 2, a=1, b=2), 'val'), **{})
The previous setitem call is equivalent to
setitem(K(1, 2, a=1, b=2), 'val')
where K
is a new class, introduced by package kwkey
.
Discussion
For discussion of PEP 472 and PEP 637, visit the Python ideas miling list.
To test installation, do
$ python3 -m kwkey.test
kwkey TestResults(failed=0, attempted=15)
kwkey.duality TestResults(failed=0, attempted=1)
kwkey.example_jfine TestResults(failed=0, attempted=7)
kwkey.jfine TestResults(failed=0, attempted=0)
kwkey.sdaprano TestResults(failed=0, attempted=0)
kwkey.test TestResults(failed=0, attempted=0)
kwkey.test_duality TestResults(failed=0, attempted=65)
kwkey.test_jfine TestResults(failed=0, attempted=19)
kwkey.test_K TestResults(failed=0, attempted=8)
kwkey.test_sdaprano TestResults(failed=0, attempted=25)
kwkey.tools TestResults(failed=0, attempted=0)
and check for no failures. (The number of tests will change over time.)
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 Distribution
Built Distribution
File details
Details for the file kwkey-0.0.2.tar.gz
.
File metadata
- Download URL: kwkey-0.0.2.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f24be6a964044ce0b3078bd5f1b2769d9f3ae163b82ab9a40fa8f4158e3d402 |
|
MD5 | b7f295e13533e08b8397185cf656333b |
|
BLAKE2b-256 | 63264ab9cc02568e5f1d8cd6e6798a3a1ef4397a85424e3f9fa837f742b7529a |
File details
Details for the file kwkey-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: kwkey-0.0.2-py3-none-any.whl
- Upload date:
- Size: 13.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 786cfd9518ad104a6c5e174a4700c8cc79a9886e4ed65f4a7a49b11865a607b5 |
|
MD5 | bc84ec348111a9b3078a77359bc3f3fd |
|
BLAKE2b-256 | 16cb718d1483702826631cd190acd388f2640190a369dc3474b723cb003040c9 |