[Python-ideas] PEP pre-draft: Support for indexing with keyword arguments
Stefano Borini
stefano.borini at ferrara.linux.it
Fri Jul 4 20:10:51 CEST 2014
On Wed, Jul 02, 2014 at 12:36:48AM +0200, Stefano Borini wrote:
> https://github.com/stefanoborini/pep-keyword/blob/master/PEP-XXX.txt
I just added a new strategy. This one cuts the problem down.
Strategy 4: Strict dictionary
-----------------------------
This strategy accepts that __getitem__ is special in accepting only one object,
and the nature of that object must be non-ambiguous in its specification of the
axes: it can be either by order, or by name. As a result of this assumption,
in presence of keyword arguments, the passed entity is a dictionary and all
labels must be specified.
C0. a[1]; a[1,2] -> idx = 1; idx=(1, 2)
C1. a[Z=3] -> idx = {"Z": 3}
C2. a[Z=3, R=4] -> idx = {"Z"=3, "R"=4}
C3. a[1, Z=3] -> raise SyntaxError
C4. a[1, Z=3, R=4] -> raise SyntaxError
C5. a[1, 2, Z=3] -> raise SyntaxError
C6. a[1, 2, Z=3, R=4] -> raise SyntaxError
C7. a[1, Z=3, 2, R=4] -> raise SyntaxError
Pros:
- strong conceptual similarity between the tuple case and the dictionary case.
In the first case, we are specifying a tuple, so we are naturally defining
a plain set of values separated by commas. In the second, we are specifying a
dictionary, so we are specifying a homogeneous set of key/value pairs, as
in dict(Z=3, R=4)
- simple and easy to parse on the __getitem__ side: if it gets a tuple,
determine the axes using positioning. If it gets a dictionary, use
the keywords.
- C interface does not need changes.
Cons:
- degeneracy of a[{"Z": 3, "R": 4}] with a[Z=3, R=4], but the same degeneracy exists
for a[(2,3)] and a[2,3].
- very strict.
- destroys the use case a[1, 2, default=5]
i
More information about the Python-ideas
mailing list