[Python-ideas] Match statement brainstorm

Nick Coghlan ncoghlan at gmail.com
Tue May 24 08:10:34 EDT 2016


On 24 May 2016 at 18:25, Paul Moore <p.f.moore at gmail.com> wrote:
> On 24 May 2016 at 07:08, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>>> On Mon, May 23, 2016 at 7:57 PM, Michael Selik <michael.selik at gmail.com>
>>> wrote:
>>>
>>>> def demo(arg):
>>>>    if p, q ?= arg.x, arg.y:                        # dict structure
>>>>    elif x ?= arg.x and isinstance(x, int)          # assignment + guard
>>>>    elif a, b, *_ ?= arg:                           # tuple structure
>>>>    elif isinstance(arg, Mapping):                  # nothing new here
>>
>>
>> I'm unenthusiastic about this -- the above looks like
>> an unreadable mess to me.
>
> Agreed - the ?= operator looks like noise to me. One of the advantages
> of a switch/case statement is that it's keyword based, which IMO is
> inherently more readable.
>
> If we keep getting sucked into comparisons with "chain of if"
> constructs, maybe the problem is that the "switch" keyword is too
> strongly associated with that in people's minds? Maybe we could focus
> on the fact that it's matching that we're doing and make it a "match"
> statement? So
>
>     match expr:
>         argspec: xxx
>         argspec:
>             yyy
>         else:
>             default

New keywords that collide with standard library APIs (re.match in this
case) are pretty much right out :)

I suspect you're right that switch/case have too much baggage to be
viable candidates for this use case, though.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list