[Python-ideas] Match statement brainstorm

Michel Desmoulin desmoulinmichel at gmail.com
Tue May 24 10:59:55 EDT 2016

What about making it a function ?

Pattern matching is complex, it can be like a mini language inside the
language, just like regex.

To match text we do:

import re
re.match('pattern', 'string')

We could do the same for matching structures:

from inspect import match

def do(stuff):
    m = match(stuff): # m implements __call__
    if m('whatever mini language you like'):
        return foo
    if m('again'):
        return m.thing_your_extracted


- no need for a new syntax
- very explicit
- use well know constructs
- we can easily start experimenting with the matching language in a lib
in Pypy


- much more verbose;
- debugging your mini language and handling error is not as good;

Le 24/05/2016 16:35, Paul Moore a écrit :
> On 24 May 2016 at 14:31, Michael Selik <michael.selik at gmail.com> wrote:
>> On Tue, May 24, 2016 at 2:08 AM 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.
>> Which is unreadable: ``if/elif`` keywords or ``?=`` operator? If it's the
>> latter, please don't get hung up on that, as I intended that as a
>> placeholder for whatever operator or keyword is best. My main point is that
>> switch/case/matching is semantically identical to if/elif, so why use
>> something different?
> For me, it's the use of operators (punctuation) plus the fact that the
> overall layout doesn't in any way imply to me "I'm looking at *this*
> expression. Let's try a sequence of matches..."
> The key distinguishing feature for me of a switch/match statement is
> that it organises the "flow" of the statement differently from
> if/elif. The if/elif chain says "try this, then that, now something
> else". There's no implication that the tests are all looking at the
> same subject - to spot that, you need to be able to see (from the
> layout of the tests) the actual subject item on each line, and the ?=
> operator syntax obscures that because "arg" is used differently in
> each test.
> With a switch statement, however, the subject is stated once, at the
> top of the statement. The checks are then listed one after the other,
> and they are all by definition checks against the subject expression.
> And in fact, for me that's the distinguishing feature of a switch
> statement - that it's a series of tests against a single implied
> subject. That's also (I suspect) why it's hard to come up with a good
> syntax for tests - Python doesn't really do "implied subjects"
> anywhere else (explicit is better than implicit and all that).
> IMO, "Series of tests against a single expression" is a common enough
> pattern to be worth exploring in spite of the fact that it runs
> counter to EIBTI. But you may need to be Dutch to understand why :-)
> Paul
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/

More information about the Python-ideas mailing list