On Tue, Sep 1, 2020, 8:35 PM Guido van Rossum <gu...@python.org> wrote:

> On Tue, Sep 1, 2020 at 4:57 PM Greg Ewing <greg.ew...@canterbury.ac.nz>
> wrote:
>
>> On 2/09/20 2:24 am, Steven D'Aprano wrote:
>> > On Sun, Aug 30, 2020 at 05:49:50PM +1200, Greg Ewing wrote:
>> >> On 30/08/20 3:06 pm, Steven D'Aprano wrote:
>> >>> On Thu, Aug 27, 2020 at 11:13:38PM +1200, Greg Ewing wrote:
>> >>>
>> >>>>     a[17, 42]
>> >>>>     a[time = 17, money = 42]
>> >>>>     a[money = 42, time = 17]
>>  >
>
> I agree it's a fine use case. Using the currently prevailing proposal
> (which I steadfastly will refer to as "Steven's proposal") it's quite
> possible to implement this.
>

> --
> --Guido van Rossum (python.org/~guido)
>

Can someone tell me: what will be the canonical way to allow both named or
unnamed arguments under Steven's proposal?

With function call syntax, it is trivial:

def f(time, money): ...

The interpreter handles it all for us:

>>> f(17, 42)
>>> f(17, money=42)
>>> f(time=17, money=42)
>>> f(money=42, time=17)

But what is the way when we can't fully benefit from python function
signature syntax?

Here's my attempt, it is probably lacking. I'm five years into a
self-taught venture in python... If I can't get this right the first time,
it worries me a little.


MISSING=object()

def __getitem__(self, key=MISSING, time=MISSING, money=MISSING):
    if time is MISSING or money is MISSING:
        if time is not MISSING and key is not MISSING:
            money = key
        elif money is not MISSING and key is not MISSING:
            time = key
        else:
            time, money = key

Is this right? Wrong? The hard way? The slow way?

What about when there are three arguments? What then?
_______________________________________________
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/DS4FBFZFPZPS3YSKC2L2ZAV72QBTSPB3/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to