[Python-ideas] Accepting keyword arguments for __getitem__

Stefano Borini stefano.borini at ferrara.linux.it
Mon Jun 23 14:06:05 CEST 2014

Dear all,

At work we use a notation like LDA[Z=5] to define a specific level of accuracy for our evaluation. This notation is used
just for textual labels, but it would be nice if it actually worked at the scripting level, which led me to think to the following:
at the moment, we have the following 

>>> class A:    
...     def __getitem__(self, y):
...         print(y)
>>> a=A()
>>> a[2]
>>> a[2,3]      
(2, 3)
>>> a[1:3]
slice(1, 3, None)
>>> a[1:3, 4]
(slice(1, 3, None), 4)

I would propose to add the possibility for a[Z=3], where y would then be a
dictionary {"Z": 3}. In the case of a[1:3, 4, Z=3, R=5], the value of y would
be a tuple containing (slice(1,3,None), 4, {"Z": 3}, {"R": 5}). This allows to
preserve the ordering as specified (e.g. a[Z=3, R=4] vs a[R=4, Z=3]).

Do you think it would be a good/useful idea? Was this already discussed or proposed in a PEP?
Google did not help on this regard.

Thank you,

Stefano Borini

More information about the Python-ideas mailing list