On Mon, Aug 3, 2020 at 2:35 PM Todd <toddr...@gmail.com> wrote:

> On Mon, Aug 3, 2020, 17:13 Guido van Rossum <gu...@python.org> wrote:
>
>> On Mon, Aug 3, 2020 at 1:49 PM Christopher Barker <python...@gmail.com>
>> wrote:
>>
>>>
>>> Yes, that would be correct. However, the function could instead be
>>>> defined as:
>>>>
>>>> def __getitem__(self, index, /, **kwargs):
>>>>     ...
>>>>
>>>> and then there'd be no conflict (as "self" and "index" must be passed
>>>> positionally). In effect, the naive spelling (which permits self and
>>>> index to be passed as keywords) would be a subtle bug that could
>>>> easily be corrected.
>>>>
>>>
>>> sure, but it would be a bug in a LOT of existing code!
>>>
>>> I wonder, if this were to be introduced, if the interpreter could have a
>>> special case that would call __getitem__ in a special way to avoid this bug
>>> in old code.
>>>
>>
>> Good edge case to consider. But would it really be such a problem? If you
>> have an existing class like this:
>>
>>     class C:
>>         def __getitem__(self, index): ...
>>
>>     c = C()
>>
>> then presumably calling `c[1, index=2]` would just be an error (since it
>> would be like attempting to call the method with two values for the `index`
>> argument), and ditto for `c[1, 2, 3, index=4]`. The only odd case might be
>> `c[index=1]` -- but presumably that would be equivalent to `c[(), index=1]`
>> so it would still fail.
>>
>
> The problem is that knowing what labels are not allowed would require
> knowing the implementation details of the dunder methods of each individual
> class, which I don't think is typically considered public API as far as I
> am aware.
>
>>
I don't understand what you're arguing; I think you didn't follow my
argument here completely.

My point is that all existing `__getitem__` implementations will raise
errors if any keywords are given, even if the keyword happens to correspond
to the name of the argument (say, `index`). This is to counter Chris B's
concern that if an existing `__getitem__` implementation didn't use the '/'
notation to indicate that `self` and `index` are positional, it would have
a bug. I claim that the bug will *only* be present when someone adds
keyword support to their `__getitem__` method without using the '/'. Since
that's not existing code, this "proves" that adding this feature would not
introduce a subtle bug in a lot of existing code -- only in carelessly
written new (or updated) code.

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/426C5KDJHHRF77NAFYEI2LE3VC3DBHEH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to