[Python-ideas] Pattern Matching Syntax

Jacco van Dorp j.van.dorp at deonet.nl
Thu May 3 10:02:15 EDT 2018


> # Pattern matching with guards
> x = 'three'
>
> number = match x:
>    1 => "one"
>    y if y is str => f'The string is {y}'
>    _ => "anything"
>
> print(number)  # The string is three

I think you meant to use isinstance(y, str) ?
This looks like an incomplete ternary as well, missing the else
clause, so it wouldn't be a valid expression either way.
And a NameError. y is never defined anywhere. Since there's no name
bound to the variable being matched, would that mean a new keyword ?
Also, in the rest you seem to be testing "x == {matchidentifier}", but
here it suddenly looks like a boolean True would trigger a match ? And
if so, would other boolean truthy values also trigger one, making the
entire construct rather....limited ?

It looks like you're attempting to suggest at least 3 new language
syntax elements here. 4 if I count the type matching of "x:int", which
you could sell as type annotation, if those hadn't been explicitly
optional and ignored when actually binding the names.

And almost every other example can be solved with a dict and .get().
The remainder uses a dict as match, and still work on if/elif
perfectly fine.

Also, I'd say "but other people do it" isn't a valid reason for
implementation. There's plenty people doing stupid things, that
doesn't mean it's a good idea to do it to. If they idea can't stand on
it's own, it's not worth it.

>From the syntax corner, it also doesn't really look like Python to me.

(my apologies if I sound a bit hostile. I've attempted 3 rewrites to
get that out. I only really tried to look at the syntax with what I
suppose is it's intended meaning here.)

2018-05-03 15:36 GMT+02:00 Alberto Berti <alberto at metapensiero.it>:
>>>>>> "Stéfane" == Stéfane Fermigier <sf at fermigier.com> writes:
>
>     Stéfane> On Thu, May 3, 2018 at 2:41 PM, Robert Roskam <raiderrobert at gmail.com>
>     Stéfane> wrote:
>     >>
>     >> And for the sake of completeness, here are other languages with similar
>     >> syntax features and their associated documentation [...]
>     >>
>     Stéfane> Still for the sake of completeness, and without any judgement from me at
>     Stéfane> this point, a couple more, which are more related to Python:
>
>     Stéfane> Coconut: http://coconut.readthedocs.io/en/master/DOCS.html#match
>
>     Stéfane> Mochi: https://github.com/i2y/mochi#pattern-matching
>
> There's also macropy http://macropy3.readthedocs.io/en/latest/pattern.html
> --
> Alberto Berti -  Information Technology Consultant
>
> "gutta cavat lapidem"
>
> _______________________________________________
> 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