[Python-ideas] Accepting keyword arguments for __getitem__

Joseph Martinot-Lagarde joseph.martinot-lagarde at m4x.org
Mon Jun 23 20:35:34 CEST 2014


Le 23/06/2014 20:22, Joseph Martinot-Lagarde a écrit :
> Le 23/06/2014 14:06, Stefano Borini a écrit :
>> 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/
>>
> Actually I proposed a similar functionnality a few months ago:
> http://thread.gmane.org/gmane.comp.python.ideas/27584
>
> The main point is not saving a few keystrokes but increase readability.
> It is indeed possible to use __call__ (that's what I'm doing in some
> cases), but then the indexing part is lost. Using a dictionnary is not
> clear either. Compare:
>
> table[x=8, y=11]
> table[{x: 8}, {y: 11}]
>
> You could argue that keyword arguments are useless since you can always
> add a dictionary as last argument...
>
> Before using python I was using Matlab. One very annoying thing in
> Matlab is that both indexing and function call use parenthesis. Code
> mixing both is really hard to understand. Coming to python was a relief
> on this aspect, where [] and () makes really clear whether the operation
> is a call or indexing.
>
> Now that I know python better, it bothers me that indexing doesn't have
> the same semantics a a function call. To me their intentions are
> different but their use should be the same. I guess that the equivalence
> between a[1, 2] and a[(1, 2)] is for backward compatibility, but it
> shouldn't stop from adding keywords arguments.
>
> Using a preprocessor seems fine when building a full application, but is
> really impracticable when crunching numbers from scripts or ipython.
> Also, using a preprocessor for something as simple as indexing seems
> really overkill.
>
> Now, I don't understand why you need to know the ordering of keyword
> arguments, since they are clearly labeled ? I'd hate to have to manually
> parse (slice(1,3,None), 4, {"Z": 3}, {"R": 5}).
>
> Joseph
>
> _______________________________________________
> 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/
>
I forgot to add that slice notation can't be used in function calls, you 
have to use the very less readable slice() function.

For a live example of something that would use keyword arguments in 
__getitem__, there is numpy.r_ : 
http://docs.scipy.org/doc/numpy/reference/generated/numpy.r_.html.

Joseph



More information about the Python-ideas mailing list