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

Stefano Borini stefano.borini at ferrara.linux.it
Thu Jul 3 20:30:59 CEST 2014


On Thu, Jul 03, 2014 at 07:15:09PM +0200, Stefano Borini wrote:
> 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 committed and pushed the most recent changes and they are now available.
> Some points have been clarified and expanded. Also, there's a new section about
> C interface compatibility. Please check the diffs for tracking the changes.

Forgot: I also added a possibility P4 for the first strategy: keyword
(alternative name "keyindex") which was proposed in the thread.
This solution would look rather neat

>>> a[3]
3
>>> a[3:1]
slice(3, 1, None)
>>> a[slice(3,1,None)]         # <- Note how this notation is a long and equivalent form of the
slice(3, 1, None)              #    syntactic sugar above
>>> a[z=4]                     # <- Again, note how this notation would be a syntactic sugar 
keyindex("z", 4)               #    for a[keyindex("z", 4)]
>>> a[z=1:5:2]                 # <- Supports slices too.
keyindex("z", slice(1,5,2))    #    No ambiguity with dictionaries, and C compatibility is 
                               #    straightforward
>>> keyindex("z", 4).key
"z"
                            
Another thing I observed is that the point of indexing operation is indexing,
and a keyed _index_ is not the same thing as a keyed _option_ during an
indexing operation. This has been stated during the thread but it's worth to
point out explicitly in the PEPi (it isn't). Using it for options such as
default would technically be a misuse, but an acceptable one for... broad
definitions of indexing.

The keyindex object could be made to implement the same interface as its value
through forwarding, so it can behave just as its value if your logic cares only about
position, and not key

>>> keyindex("z", 4) + 1
5

Another rationalization: current indexing has only one degree of freedom, that
is: positioning. Add keywords and now there are two degrees of freedom: position
and key. How are these two degrees of freedom supposed to interact?




More information about the Python-ideas mailing list