This is a continuation of my previous post. I wrote:

Here's an example of how the new dunder might work in practice.
>
>     class A:
>         __keyfn__ = None
>         def __setitem__(self, val, x=0, y=0, z=0):
>             print((val, x, y, z))
>
>     >>> a = A()
>     >>> a[1, z=2] = 'hello'
>     ('hello', 1, 0, 2)
>

 To continue, suppose that True is the default value for __keyfn__.
Consider now

    class C:
        __keyfn__ = True
        def __setitem__(self, *argv, **kwargs):
            print(f'argv={argv} | kwargs={kwargs}')

Here's one option for what should happen.

    >>> c = C()

    >>> c[1] = 'val'
    argv=(1, 'val') | kwargs={}
    >>> c[1, 2] = 'val'
    argv=((1, 2), 'val') | kwargs={}

    >>> c[a=1] = 'val'
    TypeError: __keyfn__ got unexpected keyword argument 'a'

By the way, I've not tested this code.

In short, the present behaviour continues, except that that the compile
time error
    >>> c[a=1] = 'val
    SyntaxError: invalid syntax
is replaced by the run-time error
    >>> c[a=1] = 'val'
    TypeError: __keyfn__ got unexpected keyword argument 'a'

Some of us want
    >>> d = dict()
    >>> d[a=1] = 'val'
to raise an exception. I've just described how having True as the default
value for __keyfunc__ allows that to happen, should the Python community so
decide.

I hope this message helps.
-- 
Jonathan
_______________________________________________
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/RNQFT4USRCTYTSLAUNPTWGMC6WLZEPKH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to