[Python-ideas] Accepting keyword arguments for __getitem__

Devin Jeanpierre jeanpierreda at gmail.com
Mon Jun 23 20:37:37 CEST 2014


What about using slices instead?

>>> a['Z': 3, 'B': 2]
(slice('Z', 3, None), slice('B', 2, None))

-- Devin

On Mon, Jun 23, 2014 at 5:06 AM, Stefano Borini
<stefano.borini at ferrara.linux.it> wrote:
> 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]
> 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
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list