[Python-ideas] PEP pre-draft: Support for indexing with keyword arguments

Akira Li 4kir4.1i at gmail.com
Wed Jul 2 17:14:47 CEST 2014


Stefano Borini <stefano.borini at ferrara.linux.it>
writes:

> Dear all,
>
> after the first mailing list feedback, and further private discussion
> with Joseph Martinot-Lagarde, I drafted a first iteration of a PEP for
> keyword arguments in indexing. The document is available here.
>
> https://github.com/stefanoborini/pep-keyword/blob/master/PEP-XXX.txt
>
> The document is not in final form when it comes to specifications. In
> fact, it requires additional discussion about the best strategy to
> achieve the desired result. Particular attention has been devoted to
> present alternative implementation strategies, their pros and cons. I
> will examine all feedback tomorrow morning European time (in approx 10
> hrs), and apply any pull requests or comments you may have.
>
> When the specification is finalized, or this community suggests that
> the PEP is in a form suitable for official submission despite
> potential open issues, I will submit it to the editor panel for
> further discussion, and deploy an actual implementation according to
> the agreed specification for a working test run.
>
> I apologize for potential mistakes in the PEP drafting and submission
> process, as this is my first PEP.
>

Strategy 3b: builtin named tuple

  C0. a[2] -> idx = 2; # scalar
      a[2,3] -> idx = (2, 3) # tuple
                idx[0] == 2
                idx[1] == 3
  C1. a[Z=3] -> idx = (Z=3)  # builtin named tuple (pickable, etc)
                idx[0] == idx.Z == 3 
  C2. a[Z=3, R=2] -> idx = (Z=3, R=2)
                     idx[0] == idx.Z == 3
                     idx[1] == idx.R == 2
  C3. a[1, Z=3] -> idx = (1, Z=3) 
                   idx[0] == 1
                   idx[1] == idx.Z == 3
  C4. a[1, Z=3, R=2] -> idx = (1, Z=3, R=2)
                        idx[0] == 1
                        idx[1] == idx.Z == 3
                        idx[2] == idx.R == 2
  C5. a[1, 2, Z=3] -> idx = (1, 2, Z=3)
  C6. a[1, 2, Z=3, R=4] -> (1, 2, Z=3, R=4)
  C7. a[1, Z=3, 2, R=4] -> SyntaxError: non-keyword arg after keyword arg

Pros:

 - looks nice
 - easy to explain: a[1,b=2] is equivalent to a[(1,b=2)] like a[1,2] is
   equivalent to a[(1,2)]
 - it makes `__getitem__` *less special* if Python supports a builtin
   named tuple and/or ordered keyword args (the call syntax)

Cons:

 - Python currently has no builtin named tuple (an ordered collection of
   named (optionally) values) 
 - Python currently doesn't support ordered keyword args (it might have
   made the implementation trivial)

Note: `idx = (Z=3)` is a SyntaxError so it is safe to produce a named tuple
instead of a scalar.


--
Akira



More information about the Python-ideas mailing list