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

Bruce Leban bruce at leapyear.org
Thu Jul 3 09:37:45 CEST 2014


On Wed, Jul 2, 2014 at 4:40 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

> On 07/02/2014 04:10 PM, Tim Delaney wrote:
>
>>
>> I really do think that a[b=c, d=e] should just be syntax sugar for
>> a['b':c, 'd':e]. It's simple to explain, and gives
>> the greatest backwards compatibility. In particular, libraries that
>> already abused slices in this way will just continue
>> to work with the new syntax.
>>
>
> +0.5 for keywords in __getitem__
>
> +1 for this version of it



If there weren't already abuse of slices for this purpose, would this be
the first choice? I think not. This kind of abuse makes it more likely that
there will be mysterious failures when someone tries to use keyword
indexing for objects that don't support it. In contrast, using kwargs means
you'll get an immediate meaningful exception.

Tangentially, I think the PEP can reasonably reserve the keyword argument
name 'default' for default values specifying that while __getitem__ methods
do not need to support default, they should not use that keyword for any
other purpose.

Also, the draft does not explain why you would not allow defining
__getitem__(self, idx, x=1, y=2) rather than only supporting the kwargs
form. I don't know if I think it should or shouldn't at this point, but it
definitely think it need to be discussed and justified one way or the other.

--- Bruce
Learn how hackers think: http://j.mp/gruyere-security
https://www.linkedin.com/in/bruceleban
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140703/55ddb744/attachment-0001.html>


More information about the Python-ideas mailing list