[Python-ideas] Match statement brainstorm

Koos Zevenhoven k7hoven at gmail.com
Tue May 24 08:03:17 EDT 2016


On Tue, May 24, 2016 at 6:43 AM, Guido van Rossum <guido at python.org> wrote:
> On Mon, May 23, 2016 at 7:57 PM, Michael Selik <michael.selik at gmail.com>
wrote:
>> Ok, let me refocus on the idea of assign-if-you-can during matching, not
>> just sugar for try/except.
>>
>> The matching features of equality, inequality, predicates, and subtype
are
>> already available via if/elif. The matching features of destructuring,
>> assignment, and post-assignment guards need new syntax. The other
proposals
>> in this thread suggest a new keyword or keyword pair like switch/case.
>> Instead, why not extend if/elif with a new operator?
>>
>> 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
>>
>>
[...]
> This idea has some clear advantages -- it's unambiguous about the
> order of matching, and combines clearly with existing conditions. It
> also seems like it would support "recursive" matching nicely, by
> allowing you to chain additional unpacking operators. ("Recursive"
> because IIUC that's what Haskell calls matches inside matches --
> similar to nested tuple unpackings like (a, (b, c)) = in Python.)
>
> The trick is to find a good syntax for the conditional assignment;
> using ? has been rejected by this group in the past for other
> conditionalisms.

Yeah, and indeed it's not obvious how to draw the line between whether the
conditional assignment should return a truth value or raise an exception,
as I think Greg was already implying. For instance, what would `a ?=
data[0]` do, if data[0] raises, say, a TypeError. Should it be caught or be
raised? Anyway, maybe it is possible to draw that line in a reasonable way.

So here's a
​nother syntax
:

Introduce a
​new ​
keyword `given`, which
​could be used as
 a prefix operator with roughly the following properties

given a
​   ​
# True if the name a is bound to something
given b.c
​ ​
# roughly equivalent to hasattr(b, 'c')
given a, b.c
​      ​
# same as (given a and given b.c)
given d, e = a, b.c
​ ​
# like `given a, b.c` but with added assignment

Then one could do (
​expanding on
 the above demo example)

def demo(arg):
    if given p, q = arg.x, arg.y:
        # do something

    elif given x = arg.x and isinstance(x, int):
        # do somet​​hing

    elif given a, b, *_ = arg:
        # do somet​h​ing

    elif isinstance(arg, Mapping):
        # do something


​# Or something like this:
​
​

def demo2(arg):​
​    global importan​t_value
​
​
​    if not given important_value:
        important_value = compute_important_value()


​-- Koos​

​PS. I like seeing the word "brainstorm" used here. I'm not sure if I've
seen that here before. I think that means that we try to see the good parts
of the presented ideas and send further ideas and thoughts to see if we can
find something useful -- even if the presented ideas are not yet optimal.
(Of course that does not mean that the ideas should not be presented
clearly!) Anyway, in the end, if nothing is found, it is easy to just drop
the whole concept.
​

> --
> --Guido van Rossum (python.org/~guido)
> _______________________________________________
> 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/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160524/f001ecd2/attachment.html>


More information about the Python-ideas mailing list